Home > Net >  MultiLine regex matching python
MultiLine regex matching python

Time:10-26

Below is my multi-line string.

sample='''
blah
blah
blah
blah
text.get(
'hi'
'you're
'welcome'
)
text.get(
'hi'
'i'm
'here'
> )
blah
blah
blah

I want to match based on the symbol > and get the relevant text in the in the brackets

text.get(
'hi'
'i'm
'here'
> )

I tried this code text(. )\((.*?)>(.*?)\) and it matches both the instances of text.get.Can someone please help on this

CodePudding user response:

Yoy can use

\btext\.\w \([^()]*\n> \)

See the regex demo. Details:

  • \b - a word boundary
  • text\. - a text. substring
  • \w \( - one or more word chars and then an open parenthesis
  • [^()]* - zero or more chars other than parentheses
  • \n> \) - a newline, space and close parenthesis.

If you need to capture unknown parts of the match add the groups, e.g.

\btext\.(\w )\(([^()]*)\n> \)

In Python, do not forget to use the raw string literal: r'...'.

  • Related