开发者

Match regexp in java

开发者 https://www.devze.com 2022-12-12 06:10 出处:网络
String text = \"[! hello ¡world ¡] otra cosa ¡]\"; String pt = \"\\\\[!(.*)¡\\\\]\"; Matcher mc = Pattern.compile(pt).matcher(text);
String text = "[! hello ¡world ¡] otra cosa ¡]";
String pt = "\\[!(.*)¡\\]";
Matcher mc = Pattern.compile(pt).matcher(text);
while( mc.find() ){
    System.out.println(mc.group(1));
}

This code prints hello ¡world ¡] otra cosa.

What would be a pattern that matches only hello ¡world?

Wha开发者_C百科t I don't find is a way to negate a literal string instead of just a char. Something like: ([^(¡\])]*)

The question is:

How to match everything that is NOT a literal string?


Just add a ? after the *

String pt = "\\[!(.*?)¡\\]";


You need a shy or reluctant (non-greedy) expression.

See the documentation for the java.util.regex.Pattern class for the Greedy Quantifier syntax. In your case, you want your Kleene star to match the shortest string possible. The normal behavior is greedy: it matches the longest string possible.


To answer your direct question, the way to only match . when it is not part of the string ¡] is to use a negative look-ahead:

String pt = "\\[!((?:(?!¡\\]).)*)¡\\]";


An alternative way to match the string would be to use negation so that you can avoid .*? which can cause undesirable results and slow performance.

String pt = "\[!([^]]*)¡\]";

This version will match everything up until it finds the bracket and only backtrack once to match the ¡.

0

精彩评论

暂无评论...
验证码 换一张
取 消