Björn Pollex
Björn Pollex

Reputation: 76778

Substring is found, but regex fails

Suppose I have some string, and run the following tests on it:

response.indexOf("</p:panelGrid>");
response.matches(".*</p:panelGrid>.*");

How is it possible that indexOf finds the substring (it does not return -1), but the regular expression in the second test does not match?

I have come across this problem while trying to write a test that checks if taglibs are rendered correctly in JSF with Pax Web. I have not been able to reproduce this behavior outside of this test.

Upvotes: 5

Views: 81

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200148

The . matches everything except for newline characters. You must change your regex string to

"(?s).*</p:panelGrid>.*"

Then it will match always.

Upvotes: 7

Related Questions