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.
RegExp provides more flexibility. To check for substrings in a string with regex can be done with the test() method of regular expression. This method is much easier to use as it returns a boolean.
$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.
Note: If there is no match, the value None will be returned, instead of the Match Object. The Match object has properties and methods used to retrieve information about the search, and the result: . span() returns a tuple containing the start-, and end positions of the match.
Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.
The .
matches everything except for newline characters. You must change your regex string to
"(?s).*</p:panelGrid>.*"
Then it will match always.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With