简体   繁体   中英

Python regex to search substring in multiline string variable

I am trying to search for group of words in a multiline string variable. I can do this by

>>> test_1 = """--Comment
... /*Comment*/
... MERGE fhkjfsk lkfjs;lfks;f
... hdkjdkd
... fjlkf
... """
>>> m = re.search(r"(insert|merge|update|delete)\s*", test_1, re.IGNORECASE)
>>> print(m)
<_sre.SRE_Match object; span=(22, 28), match='MERGE '>

But I want to match only those words that are present in a line that doesn't start with "--" or "/*". So I don't want following to match:

>>> test_1 = """--Comment
... /*Comment*/
... --MERGE fhkjfsk lkfjs;lfks;f
... hdkjdkd
... fjlkf
... """
>>> m = re.search(r"(insert|merge|update|delete)\s*", test_1, re.IGNORECASE)
>>> print(m)
<_sre.SRE_Match object; span=(24, 30), match='MERGE '>

This is what I am unable to get to work. I have tried:

>>> test_1 = """--Comment
... /*Comment*/
... --MERGE fhkjfsk lkfjs;lfks;f
... INSERT fhskjfsf
... gfdsjhs
... """
>>> m = re.search(r"^(?!--)(insert|merge|update|delete)\s*", test_1, re.IGNORECASE)
>>> print(m)
None
>>>

I think I need something in between the two parenthesis (?!--)(insert|merge|update|delete) and have tried adding

.+ like (?!--).+(insert|merge|update|delete)

\s* like (?!--)\s*(insert|merge|update|delete)

. like (?!--).(insert|merge|update|delete)

.* like (?!--).*(insert|merge|update|delete) but it doesn't help.

Any suggestions on how to get this working would be greatly appreciated.

The regex is fine, but the ^ matches only the start of the whole string. You should use re.MULTILINE to make it match starts of lines.

re.search(r"^(?!--)(insert|merge|update|delete)\s*", test_1, re.IGNORECASE | re.MULTILINE)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM