jueves, 22 de abril de 2010

Whole word search en java usando regex


Pattern myPattern = Pattern.compile("\\bSebastián Piñera\\b",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE
| Pattern.UNICODE_CASE | Pattern.CANON_EQ);

String text;
Matcher matcher;

text = "de la pradera camión\nSebastián Piñera";
matcher = myPattern.matcher(text);
System.out.println(matcher.find());

text = "clorox de la pradera Sebastián Piñera jaja";
matcher.reset(text);
System.out.println(matcher.find());

text = "clorox de la pradera jaja";
matcher.reset(text);
System.out.println(matcher.find());


(Ojo con el uso eficiente del "matcher" mediante el método "reset")

Imprimirá:


true
true
false


Ref: http://www.regular-expressions.info/java.html

No hay comentarios: