[英]Java Regex: Match string contains any of two texts among many
Want to match a string containing 要匹配包含的字符串
(text1 or text2) and ( text3 or text4) and (text5) and (any of two among text6,text7,text8 and text9)
So far I have tried 到目前为止,我已经尝试过
^(?=.*(text1|text2))(?=.*(text3|text4))(?=.*text5).*$
which matches a string contains (text1 or text2) and ( text3 or text4) and (text5) 匹配包含(text1或text2)和(text3或text4)和(text5)的字符串
How to write a single pattern which fulfills my need. 如何编写一个满足我需要的模式。
**PS:**I don't want to divide the last part(any of two among text6,text7,text8 and text9) and write regex like ** PS:**我不想将最后一部分(text6,text7,text8和text9中的任何一部分)划分为正则表达式,例如
(^(?=.*(text1|text2))(?=.*(text3|text4))(?=.*text5)(?=.*text6)(?=.*text7).*$)|(^(?=.*(text1|text2))(?=.*(text3|text4))(?=.*text5)(?=.*text6)(?=.*text8).*$)|(^(?=.*(text1|text2))(?=.*(text3|text4))(?=.*text5)(?=.*text6)(?=.*text9).*$)|(^(?=.*(text1|text2))(?=.*(text3|text4))(?=.*text5)(?=.*text7)(?=.*text8).*$)|(^(?=.*(text1|text2))(?=.*(text3|text4))(?=.*text5)(?=.*text7)(?=.*text9).\*$)|(^(?=.*(text1|text2))(?=.*(text3|text4))(?=.*text5)(?=.*text8)(?=.*text9).*$)
Since i don't know how many texts i gonna have in my last part. 由于我不知道我最后一部分将要包含多少文本。
Any suggestions would be highly appreciated. 任何建议将不胜感激。
Just change your regex like below. 只需如下更改您的正则表达式即可。
^(?=.*(text[12]))(?=.*(text[34]))(?=.*text5)(?=.*(text[6789]))(?=.*(?:(?!\3)text[6789])).*$
Java regex would be, Java正则表达式将是
"^(?=.*(text[12]))(?=.*(text[34]))(?=.*text5)(?=.*(text[6789]))(?=.*(?:(?!\\3)text[6789])).*$"
(?=.*(text[6789]))(?=.*(?:(?!\\3)text[6789]))
Asserts that there must be be two text[6789]
or text[6789]
but the text strings must not be repeated. (?=.*(text[6789]))(?=.*(?:(?!\\3)text[6789]))
断言必须有两个text[6789]
或text[6789]
但文本字符串不能重复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.