代码块的 Python 正则表达式? [英] Python regex for blocks of code?
问题描述
如何创建一个正则表达式,它可以匹配一行的开头以及以下所有以 tab 开头的行?例如
不是关键字;不是这条线;关键字和随机文本;这条线;这条线;和这条线;不是关键字;
我希望能够匹配从 '^keyword' 到 'and this line ;'
谢谢.
编辑.我正在尝试为不需要的节点删除 Maya 的 MEL 代码.实际代码如下所示,多行 setAttr 和制表符缩进.
createNode mentalrayOptions ...........设置属性………………
我将整个文本加载到 1 个变量中
with open( 'path/to/file', 'r') as content_file:内容 = content_file.read()
我尝试的正则表达式似乎正确地找到了起点,但我无法正确地获得终点.它要么匹配 1 行,根本不匹配任何内容,要么一直匹配到文件末尾.
match = re.search( r'(^createNode mentalrayOptions)(.*\n)(^\t)' ,content, flags=re.DOTALL)
你可以这样使用:
^keyword.*(?:\n^\t.*)*
标志:
m
用于 mutiline,所以^
有效.- not
s
所以.
不匹配换行符.
说明:
^keyword
- 以keyword
开头的行.*
- 匹配到行尾(?:\n^\t.*)*
- 每一个都匹配以制表符开头的另一行.请注意,我们需要匹配换行符,因此请注意是否有其他行分隔符.
工作示例:http://www.regex101.com/r/jP8yH0>
当然,如果您尝试匹配真实代码块,这可能会很快失败 - 例如通过注释或字符串文字.
How do I created a regex that can match a start of a line and also all the following lines starting with 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 ;'
Thank you.
edit. I'm trying to remove Maya's MEL code for the node I don't need. The actual code looks like this, with multiple lines of setAttr with tab indent.
createNode mentalrayOptions ......... ;
setAttr .............. ;
I load the entire text into 1 variable with
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.
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.- not
s
so.
does not match new-lines.
Explanation:
^keyword
- Start of the line withkeyword
.*
- match until the end of the line(?:\n^\t.*)*
- each of these matches another line that begins with a tab. 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
Naturally, if you are trying to match real blocks of code, this can fail quickly - for example by comments or string literals.
这篇关于代码块的 Python 正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!