多行正则表达式匹配 python

MultiLine regex matching python

下面是我的多行字符串。

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

我想根据符号>进行匹配,得到括号内的相关文字

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

我试过这段代码 text(.+)\((.*?)>(.*?)\) 并且它与 text.get.Can 的两个实例都匹配 请有人帮助解决这个问题

年可以使用

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

参见regex demo详情:

  • \b - 单词边界
  • text\. - text. 子串
  • \w+\( - 一个或多个单词字符,然后是一个左括号
  • [^()]* - 除括号外的零个或多个字符
  • \n> \) - 换行符,space 和右括号。

如果您需要捕获匹配的未知部分,请添加组,例如

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

在 Python 中,不要忘记使用原始字符串文字:r'...'.