匹配失败的行为

从上一次匹配的起始位置的下一位继续尝试匹配:当正则表达式的匹配失败时,通常会从当前匹配的起始位置的下一位开始尝试匹配。这意味着如果某个字符不匹配,正则引擎会移动到下一个字符并重新开始匹配。

不同输入方式的换行符\n

实际换行符:在字符串中使用 \n 会导致换行。
字面字符:在字符串中使用 \\n 会被视为普通字符,不会产生换行效果。

针对两种不同输入方式应该用不同正则

1
2
3
4
5
6
7
def regex1(code):
# 使用正则表达式匹配printf中的字符串 匹配 | (" \n " *) |
# (代码内读取)实际换行符
# modified_code = re.sub(r'(\s*\"[^\"\\n]*)(\n)([^\"]*\"\s*[^\)]*\))', lambda m: m.group(1) + '\\n' + m.group(3), code)
# (外文件读取)字面\n
modified_code = re.sub(r'(\s*\"[^\"\\n]*)(\\n)([^\"]*\"\s*[^\)]*\))', lambda m: m.group(1) + '\\n' + m.group(3), code)
return modified_code

(代码内读取)实际换行符

1
2
3
4
5
6
7
8
9
# 代码内读取
in2 = """
printf("%d: pte %p pa %p\n",i,pte,child); // [!code ++]
printf("This is a test\n"); // [!code --]
printf("No newline here"); // [!code ++]
"""

in2 = regex1(in2)
print(in2)

(外文件读取)字面\n

1
2
3
4
5
6
7
8
# 外文件读取
work_dir = os.path.dirname(os.path.abspath(__file__)) + "\\"
# 读取输入文件
with open(work_dir + 'in2.md', 'r', encoding='utf-8') as input_file:
in2 = input_file.read()

in2 = regex1(in2)
print(in2)

没有限制默认正确

对于例子

1
printf("page table %p\n", pagetable);

使用正则

1
r'(\bprintf\s*\(\s*"[^"\n]*)(\n)([^"]*"\s*\))'