[英]Regex find whole substring between parenthesis containing exact substring
For example I have string: 例如我有字符串:
"one two (78-45ack sack); now (87 back sack) follow dollow (59 uhhaaa)"
and I need only whole substring with parenthesis, containing word "back"
, for this string it will be: 我只需要带括号的整个子串,包含单词"back"
,对于这个字符串,它将是:
"(87 back sack)"
I've tried: 我试过了:
(\(.*?back.*?\))
but it's return "(78-45ack sack); now (87 back sack)"
但它回归"(78-45ack sack); now (87 back sack)"
How should my regex look like? 我的正则表达式应该怎么样? As I understood it's happening cause search going from begin of the string, in common - how to perform regex to "search"
from the middle of string, from the word "back"
? 正如我所理解的那样,正在发生搜索从字符串的开头开始,共同点 - 如何从字符串的中间执行正则表达式"search"
,从单词"back"
?
You can use this regex based on negated character class: 您可以使用基于否定字符类的此正则表达式:
\([^()]*?back[^()]*\)
[^()]*
matches 0 or more of any character that is not (
and )
, thus making sure we don't match across the pair of (...)
. [^()]*
匹配任何不是(
和)
字符的0或更多,因此确保我们不匹配(...)
。 Alternatively, you can also use this regex based on negative regex: 或者,您也可以使用基于负正则表达式的此正则表达式:
\((?:(?!back|[()]).)*?back[^)]*\)
(?!back|[()])
is negative lookahead that asserts that current position doesn't have back
or (
or )
ahead in the text. (?!back|[()])
是否定前瞻,断言当前位置在文本中没有back
或(
或)
前面。 (?:(?!back|\\)).)*?
matches 0 or more of any character that doesn't have back
or (
or )
ahead. 匹配任何没有back
或(
或)
前面的任何角色的0或更多。 [^)]*
matches anything but )
. [^)]*
匹配任何东西但是)
。 h="one two (78-45ack sack); now (87 back sack) follow dollow (59 uhhaaa)"
l=h.split("(")
[x.split(")")[0] for x in l if ")" in x and "back" in x]
Try the below pattern for reluctant matching 尝试以下模式以进行不情愿的匹配
pattern="\\(.*?\\)" 图案= “\\(。*?\\)”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.