简体   繁体   English

多行正则表达式匹配python

[英]MultiLine regex matching python

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我试过这个代码text(.+)\\((.*?)>(.*?)\\)并且它匹配 text.get 的两个实例。有人可以帮忙吗

Yoy can use哟可以用

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

See the regex demo .请参阅正则表达式演示 Details :详情

  • \\b - a word boundary \\b - 单词边界
  • text\\. - a text. - 一个text. substring子串
  • \\w+\\( - one or more word chars and then an open parenthesis \\w+\\( - 一个或多个单词字符,然后是一个左括号
  • [^()]* - zero or more chars other than parentheses [^()]* - 除括号外的零个或多个字符
  • \\n> \\) - a newline, space and close parenthesis. \\n> \\) - 换行符、空格和右括号。

If you need to capture unknown parts of the match add the groups, eg如果您需要捕获匹配的未知部分,请添加组,例如

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

In Python, do not forget to use the raw string literal: r'...' .在 Python 中,不要忘记使用原始字符串文字: r'...'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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