[英]Match all characters except one
I have a test string with pattern 我有一个带有模式的测试字符串
{something1=any_character} any_character {something2=any_character}
I wanted to match the first bracketed sequence and in next iteration I wanted to match the next. 我想匹配第一个括号内的序列,而在下一个迭代中,我想匹配下一个。 So I am using
Pattern
and Matcher
. 所以我正在使用
Pattern
和Matcher
。
The regex I have is \\{(\\w)*something1.*(?!=).*(?!\\})
but the problem is that it match the complete string rather then just the first one. 我拥有的正则表达式是
\\{(\\w)*something1.*(?!=).*(?!\\})
但问题是它匹配完整的字符串,而不是第一个。 I want to break the first search at first }
and then find next }
. 我想突破第一搜索起初
}
然后再找下一个}
。
只是使通配符变得懒惰:
\\{(\\w)*?something1.*?(?!=).*?\\}
If you are using Pattern
and Matcher
, then only capture groups like this : 如果您使用的是
Pattern
和Matcher
,则仅捕获这样的组:
(\\\\{\\\\w+=\\\\w+\\\\})
and use a while()
loop with matcher.find()
(\\\\{\\\\w+=\\\\w+\\\\})
并使用while()
循环与matcher.find()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.