正则表达式查找 (xx 或 xx)

huangapple go评论57阅读模式
英文:

Regex find ( xx OR xx )

问题

你可以更新你的正则表达式以匹配包含OR的文本。以下是更新后的正则表达式:

\([^)]*OR[^)]*\)

这个正则表达式将匹配以(开头,然后包含OR,最后以)结尾的文本部分。

英文:

To split up some text I am trying to make a regex that covers the following:
A part of text that starts with ( untill the first ) and where OR is in between the match.

Example input 1:

(Test OR test2) some more text

Wanted match 1:

(Test OR test2)

Example input 2:

(Test OR test2 OR test3) some more text (TEST1 OR test2)

Wanted match 2:

(Test OR test2 OR test 3)

If there is no OR in the text for example

"(Test AND test2 AND test3) some more text"

Then I do not want to match.

I was able to make a regex for getting text withing ():

(\([^)]+\))

How should I update it for the above user case?

答案1

得分: 1

你可以使用以下正则表达式:

\([^()]*\bOR\b[^()]*\)

查看正则表达式演示

详细信息

  • \( - 左括号 ( 字符
  • [^()]* - 除了 () 字符之外的零个或多个字符
  • \bOR\b - 完整单词 OR
  • [^()]* - 除了 () 字符之外的零个或多个字符
  • \) - 右括号 ) 字符。
英文:

You can use

\([^()]*\bOR\b[^()]*\)

See the regex demo

Details:

  • \( - a ( char
  • [^()]* - zero or more chars other than ( and )
  • \bOR\b - a whole word OR
  • [^()]* - zero or more chars other than ( and )
  • \) - a ) char.

huangapple
  • 本文由 发表于 2023年3月31日 20:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定