[英]Python regex for blocks of code?
How do I created a regex that can match a start of a line and also all the following lines starting with tab? 如何创建可与行首以及以下所有以tab开头的行匹配的正则表达式? For example
例如
not keyword ;
not this line ;
keyword and random text ;
this line ;
this line ;
and this line ;
not keyword ;
I want to be able to match starting from '^keyword' to 'and this line ;' 我希望能够匹配从'^ keyword'到'and this line;'的内容
Thank you. 谢谢。
edit. 编辑。 I'm trying to remove Maya's MEL code for the node I don't need.
我正在尝试删除不需要的节点的Maya MEL代码。 The actual code looks like this, with multiple lines of setAttr with tab indent.
实际代码如下所示,其中包含多行setAttr和Tab缩进。
createNode mentalrayOptions ......... ;
setAttr .............. ;
I load the entire text into 1 variable with 我将整个文本加载到1个变量中
with open( 'path/to/file', 'r') as content_file:
content = content_file.read()
the regex I tried seem to find the starting point correctly but I can't get the end point correctly. 我尝试过的正则表达式似乎可以正确找到起点,但是我无法正确找到终点。 It either matches 1 line, not matching anything at all or matches all the way to the end of file.
它要么匹配1行,什么都不匹配,要么一直匹配到文件末尾。
match = re.search( r'(^createNode mentalrayOptions)(.*\n)(^\t)' ,content, flags=re.DOTALL)
You could use something like this: 您可以使用如下形式:
^keyword.*(?:\n^\t.*)*
Flags: 标志:
m
for mutiline, so ^
works. m
代表mutiline,因此^
起作用。 s
so .
s
如此.
does not match new-lines. Explanation: 说明:
^keyword
- Start of the line with keyword
^keyword
-开始与该行的keyword
.*
- match until the end of the line .*
-匹配到行尾 (?:\\n^\\t.*)*
- each of these matches another line that begins with a tab. (?:\\n^\\t.*)*
-每个都匹配以制表符开头的另一行。 Note that we hat to match the new-line, so take care if you have other line separators. Working example: http://www.regex101.com/r/jP8yH0 工作示例: http : //www.regex101.com/r/jP8yH0
Naturally, if you are trying to match real blocks of code, this can fail quickly - for example by comments or string literals. 自然,如果您尝试匹配实际的代码块,这可能会很快失败-例如通过注释或字符串文字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.