How to use regex in String.contains () method in Java

I want to check if the String contains the words "store", "store" and "product" in this order. No matter what is in between.

I tried using someString.contains(stores%store%product); , as well as .contains("stores%store%product");

Do I need to explicitly declare a regular expression and pass it to a method, or am I unable to pass a regular expression at all?

+74
java string regex
Feb 28 '13 at 7:57
source share
5 answers

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).

+84
Feb 28 '13 at 8:01
source share

matcher.find() does what you need. Example:

 Pattern.compile("stores.*store.*product").matcher(someString).find(); 
+59
Jun 03 '14 at 6:45
source share

You can simply use the matches method for the String class.

 boolean result = someString.matches("stores.*store.*product.*"); 
+10
Feb 29 '16 at 11:28
source share

If you want to check if a string contains a substring or not using regex, then the closest you can do is use find () -

  private static final validPattern = "\\bstores\\b.*\\bstore\\b.*\\bproduct\\b" Pattern pattern = Pattern.compile(validPattern); Matcher matcher = pattern.matcher(inputString); System.out.print(matcher.find()); // should print true or false. 

Note the difference between match () and find (), matches () returns true if the entire string matches the given pattern. find () tries to find a substring that matches the pattern in the given input string. In addition, with the find () function, you do not need to add an additional match, for example - (? S). * At the beginning and. * At the end of your regex pattern.

0
May 16 '17 at 21:11
source share
 public static void main(String[] args) { String test = "something hear - to - find some to or tows"; System.out.println("1.result: " + contains("- to -( \\w+) som", test, null)); System.out.println("2.result: " + contains("- to -( \\w+) som", test, 5)); } static boolean contains(String pattern, String text, Integer fromIndex){ if(fromIndex != null && fromIndex < text.length()) return Pattern.compile(pattern).matcher(text).find(); return Pattern.compile(pattern).matcher(text).find(); } 

1.result: true

2.result: true

0
Jun 01 '17 at 23:00
source share



All Articles