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. s
so .
does not match new-lines. Explanation:
^keyword
- Start of the line with keyword
.*
- 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.