python 中的正则表达式

Regular expression in python

我正在尝试 match/sub 以下行

line1 = '# Some text\n'

但要避免 match/sub 这样的行

'# Some text { .blah}\n'

因此在其他情况下,# 后跟任意数量的单词空格和数字(无标点符号),然后是行尾。

line2 = re.sub(r'# (\P+)$', r'#  { .text}', line1)

将line1的内容原样放入line2。 (我在某处读到 \P 表示除了标点符号之外的所有内容)

line2 = re.sub(r'# (\w*\d*\s*)+$', r'#  { .text}', line1)

而上面给出了

'#  { .text}'

感谢任何帮助 谢谢 汤姆

如果您只想要以 # 开头并以字母数字值、空格和 _ 继续的行,您需要这样:

/^#[\w ]+$/gm

你的正则表达式有点奇怪;展开,看起来像

r"# ([a-zA-Z0-9_]*[0-9]*[ \t\n\r\f\v]*)+$"

注意事项:

  1. 它没有锚定到字符串的开头,这意味着它会匹配

    print("Important stuff!")  # Very important
    
  2. \d*是多余的,因为它已经被\w*

  3. 捕获了
  4. 看你的例子,看来你应该不那么担心标点符号了;唯一不能拥有的是花括号 ({).

尝试

from functools import partial

def add_text(txt):
    return re.sub(r"^#([^{]*)$", r"# { .text }", txt, flags=re.M)

text = "# Some text\n# More text { .blah}\nprint('abc') # but not me!\n# And once again"

print("===before===")
print(text)
print("\n===after===")
print(add_text(text))

这给出了

===before===
# Some text
# More text { .blah}
print('abc') # but not me!
# And once again

===after===
# Some text { .text }
# More text { .blah}
print('abc') # but not me!
# And once again { .text }