我需要在每一行的“#”后面加一个字符串,所有行都有#。我已经有一个正则表达式匹配的行,当我添加评论部分到这不工作。我把第一条评论后的所有行作为一个组。在
行格式:
Line1 blah blah... }}#Comment1 or it could be
Line1 blah blah...}}# Comment1
“#”和注释之间有空格或没有空格。现在它匹配到第一个大括号。在
我的代码:
^{pr2}$我将正则表达式修改为:
Linepattern = re.compile(r'\{(\s*(\w+)\s*|(\w+)|(\w+)\s*)\{(.*?)\}\}(#.*)?', re.DOTALL)
我看了这个,它非常接近我要找的:Expression up to comment or end of line
我的输出是:
Comment1
Line2 # Comment2
Line3 # Comment3 and so on...
我的行格式:
Foo { bar { foo=0; } }# blah1 =1, blah2=1 , blah3 =1, blah#=1
FOO { bar { bar=1;bar=2; } }#comment 2
试试看这个。看到了吗演示集{cd1>标志^。在
类似于
print re.findall(r"(?<=#).+$",x,re.M)
。这里的x
是您的测试字符串。在http://regex101.com/r/uH3tP3/3
相关问题 更多 >
编程相关推荐