Using Regex in String contains method in Java example shows does the contains method support regex and the alternative ways of using the regex pattern inside contains.
Does the String contains method support a regex pattern?
No. The String contains
method does not support a regex pattern. We cannot specify the regex pattern inside the String contains
method.
Are there any alternatives so that we can use the regex to check if the string contains the specified substring? Yes, absolutely. We can either use the matches
method of the String class or we can use the Pattern and Matcher classes to do the same.
1 |
public boolean matches(String regex) |
The matches
method returns true if the regex pattern matches with the string content. The below example shows how to check if a string contains any number using the matches
method.
1 2 3 4 5 6 7 8 9 10 |
package com.javacodeexamples.regex; public class StringContainsRegex { public static void main(String[] args) { String str = "There are 100 files"; System.out.println( str.matches(".*\\d+.*") ); } } |
Output
1 |
true |
Please note that the matches
method matches the whole content of the string against the specified regex pattern. That is the reason I have specified “.*” at the beginning and at the end of the regex pattern.
We can also have multiple checks just like the contains
method.
1 2 3 4 5 6 7 8 9 10 |
package com.javacodeexamples.regex; public class StringContainsRegex { public static void main(String[] args) { String str = "There are 100 files"; System.out.println( str.matches(".*\\d+.*") && str.matches(".*files.*")); } } |
Output
1 |
true |
The above-given code checks if the string contains any number and also contains substring “files”. Now let’s consider another example.
1 2 3 4 5 6 7 8 9 10 |
package com.javacodeexamples.regex; public class StringContainsRegex { public static void main(String[] args) { String str = "pineapple"; System.out.println( str.matches(".*apple.*")); } } |
Output
1 |
true |
Just like the String contains
method, our code also matched “apple” with “pineapple”. But the good thing is since we are now using the matches
method, we can match with the whole word only if we want to.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.regex; public class StringContainsRegex { public static void main(String[] args) { String str = "pineapple"; System.out.println( str.matches(".*\\bapple\\b.*")); str = "pineapple apple"; System.out.println( str.matches(".*\\bapple\\b.*")); } } |
Output
1 2 |
false true |
I’ve used the word boundary “\b” to match with the whole word only in the second matches
method.
Instead of the matches
method, we can also use the Pattern and Matcher classes as the replacement of the String contains
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javacodeexamples.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringContainsRegex { public static void main(String[] args) { String str = "There are 100 files"; Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(str); System.out.println( matcher.find() ); } } |
Output
1 |
true |
Unlike the matches
method, the Matcher class does not match the regex pattern against the whole string. That is the reason we do not need to specify “.*” at the start and at the end of the regex pattern. We can also write this code in one line as given below.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.javacodeexamples.regex; import java.util.regex.Pattern; public class StringContainsRegex { public static void main(String[] args) { String str = "There are 100 files"; System.out.println( Pattern.compile("\\d+").matcher(str).find() ); } } |
Output
1 |
true |
However, please note that the pattern compilation is a costly operation. You should reuse the compiled pattern object if multiple checks with the same pattern are needed.
If you want to learn more about regular expression, please visit the Java Regex tutorial.
Please let me know your views in the comments section below.