Java RegEx with lookahead failing -


in java, unable regex behave way wanted, , wrote little junit test demonstrate problem:

public void testlookahead() throws exception {     pattern p = pattern.compile("abc(?!!)");     asserttrue(p.matcher("abc").find());     asserttrue(p.matcher("abcx").find());     assertfalse(p.matcher("abc!").find());     assertfalse(p.matcher("abc!x").find());     assertfalse(p.matcher("blah/abc!/blah").find());      p = pattern.compile("[a-z]{3}(?!!)");     asserttrue(p.matcher("abc").find());     asserttrue(p.matcher("abcx").find());     assertfalse(p.matcher("abc!").find());     assertfalse(p.matcher("abc!x").find());     assertfalse(p.matcher("blah/abc!/blah").find());      p = pattern.compile("[a-z]{3}(?!!)", pattern.case_insensitive);     asserttrue(p.matcher("abc").find());     asserttrue(p.matcher("abcx").find());     assertfalse(p.matcher("abc!").find());     assertfalse(p.matcher("abc!x").find());     assertfalse(p.matcher("blah/abc!/blah").find()); //fails, why?      p = pattern.compile("[a-za-z]{3}(?!!)");     asserttrue(p.matcher("abc").find());     asserttrue(p.matcher("abcx").find());     assertfalse(p.matcher("abc!").find());     assertfalse(p.matcher("abc!x").find());     assertfalse(p.matcher("blah/abc!/blah").find());  //fails, why? } 

every line passes except 2 marked comment. groupings identical except pattern string. why adding case-insensitivity break matcher?

your tests fail, because in both cases, pattern [a-z]{3}(?!!) (with case_insensitive) , [a-za-z]{3}(?!!) find @ least 1 match in "blah/abc!/blah" (they find bla twice).

a simple tests shows this:

pattern p = pattern.compile("[a-z]{3}(?!!)", pattern.case_insensitive); matcher m = p.matcher("blah/abc!/blah"); while(m.find()) {     system.out.println(m.group()); } 

prints:

bla bla 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -