[英]How to match regex avoid parentheses inside parentheses
I have string expression like this 我有这样的字符串表达式
CONTAIN("A(ASDFASDF)","MAKLOON") && !CONTAIN("THIS IS THE (STRING) ","MAKLON") && !CONTAIN("ASDFASDF","MAKLUN") && ("121"=="" || 121.00=="" || 121.0=="")
I want to match only the result like this : 我只想匹配这样的结果:
1. CONTAIN("A(ASDFASDF)","MAKLOON")
2. CONTAIN("THIS IS THE (STRING) ","MAKLON")
3. CONTAIN("ASDFASDF","MAKLUN")
I have try with this regex but the match only this : 我已经尝试过此正则表达式,但匹配仅此:
CONTAIN\(.*?\)
Result 结果
1. CONTAIN("A(ASDFASDF)
2. CONTAIN("THIS IS THE (STRING)
3. CONTAIN("ASDFASDF","MAKLUN")
How to solve my problem? 如何解决我的问题? Thanks
谢谢
您可以尝试以下模式: CONTAIN\\(.*?"\\)
Here's a slightly enhanced version, which allows the inner string to have parenthesis. 这是一个稍微增强的版本,它允许内部字符串带有括号。 It's not perfect either, but probably a bit more secure:
它也不是完美的,但可能更安全:
CONTAIN\(".*?", ?".*?"\)
Brief explanation : It matches CONTAIN(
, then any character until it finds ","
or ", "
(optional space, remove ?
if you will never have a space there), then again any character until the final ")
. The ?
after the *
is necessary to make it match as little as possible. Otherwise, .*
would match as much as possible, from the first to the last CONTAIN
string. 简要说明 :它匹配
CONTAIN(
那么任何字符,直到找到","
或", "
(可选空间,删除?
如果你将永远不会有有一个空格),然后再次任何字符,直到最后")
的。 ?
后*
是使它尽可能少匹配的必要条件,否则, .*
从第一个到最后一个CONTAIN
字符串都将尽可能匹配。
Besides matching what you mention in your post, it will also match: 除了匹配您在帖子中提到的内容之外,它还将匹配:
CONTAIN("HEL()LO",")WORLD(")
CONTAIN(")HELLO(",")WORLD(")
And will not match an invalid strings like these, which are matched by the other proposed solutions: 并且不会匹配其他建议的解决方案所匹配的无效字符串,例如:
CONTAIN("HELLO",")WORLD() // partial match
CONTAIN(")
CONTAIN(""")
I tried to do some more complex regex to match the number of quotes or parenthesis, but I think you don't need such complexity, unless your string may have escaped quotes, like \\"
or ""
. 我试图做一些更复杂的正则表达式来匹配引号或括号,但是我认为您不需要这种复杂性,除非您的字符串可能已经转义了引号,例如
\\"
或""
。
If you won't get any of those invalid strings or strange strings, you may be good enough with the other simpler regex. 如果您不会收到任何无效字符串或奇怪的字符串,那么使用其他更简单的正则表达式可能就足够了。
I think the regular expression you're looking for would be: (CONTAIN\\(\\".+\\"\\))
. 我认为您要查找的正则表达式为:
(CONTAIN\\(\\".+\\"\\))
。 By including the \\"
you get all of the characters inside the quotes instead of ending at the first instance of a ')'. 通过包含
\\"
您可以将所有字符括在引号内,而不是以')'的第一个实例结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.