Home > Enterprise >  Python regex to search substring in multiline string variable
Python regex to search substring in multiline string variable

Time:05-26

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.

CodePudding user response:

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)
  • Related