String.contains
String.contains works with string, period. It does not work with regex. It checks if the exact row is displayed in the current row or not.
Note that String.contains does not check word boundaries; it just checks the substring.
Regression solution
Regex is more powerful than String.contains , because you can force the word boundary for keywords (by the way). This means that you can search for keywords as words, not just substrings.
Use String.matches with the following regex:
"(?s).*\\bstores\\b.*\\bstore\\b.*\\bproduct\\b.*"
Regular expression RAW (remove escaping in a string literal - this is what you get when printing the line above):
(?s).*\bstores\b.*\bstore\b.*\bproduct\b.*
\b checks for word boundaries, so you wonβt get a match for restores store products . Please note that stores 3store_product also rejected, as the numbers and _ are considered part of the word, but I doubt this case appears in the natural text.
Since the word boundary is checked for both sides, the regular expression above will look for exact words. In other words, stores stores product will not match the regular expression above, because you are looking for the word store without s .
. usually matches any character except a few new line characters . (?s) at the beginning does . matches any character without exception (thanks to Tim Pitzker for pointing this out).
nhahtdh Feb 28 '13 at 8:01 2013-02-28 08:01
source share