英文:
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 wordOR[^()]*- zero or more chars other than(and)\)- a)char.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论