Table of Contents
In this post, we are going to see String’s matches method.
String’s matches method
String matches method is used to check whether the string is matched with the given regular expression.In normal terms, we can pass various regular expressions to check if the specified string matches with that regular expression.If the matches get successful return true otherwise false.
String matches method throws PatternSyntaxException if the given regular expression is not valid.
Syntax:
1 2 3 |
public boolean matches(String regex) //regex is regular expression. |
How to use matches method:
String matches method use regular expression.so, two widely used regular expression meta characters are dot (.) and astrick (). (.) which matches any character and () which matches any number of times.Make regular expression according to your pattern you want to match.
Below given example perform the following task:
• Checks if string contains any numeric digit or not ex: [0-9]
• Checks if string contains any character or not ex: [A-Za-z].
• Checks string contains given word or not ex: (john)
For all these, we can create regular expressions and check it with String’s matches method.
String matches example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package org.arpit.java2blog; public class JavaStringMatchesMain { public static void main(String args[]) { String str="Hello i am john,id is 1 and my seat number is 88"; System.out.println(str); // showing numeric pattern matching System.out.println("==================="); System.out.println("Number matching example"); System.out.println("==================="); boolean b=str.matches(".*[1-9].*"); System.out.println("pattern: .*[1-9].* matches => "+b); b=str.matches(".*(100).*"); System.out.println("pattern: .*(100).* matches => "+b); b=str.matches(".*(88).*"); System.out.println("pattern: .*(88).* matches => "+b); //showing character matching System.out.println("==================="); System.out.println("Character matching example"); System.out.println("==================="); b=str.matches(".*[A-Za-z].*"); System.out.println("pattern: .*[A-Za-z].* matches => "+b); b=str.matches(".*[A].*"); System.out.println("pattern: .*[A].* matches => "+b); b=str.matches(".*[i].*"); System.out.println("pattern: .*[i].* matches => "+b); System.out.println("==================="); System.out.println("Word and String matching example"); System.out.println("==================="); //showing word and string matching b=str.matches(".*(john).*"); System.out.println("pattern: .*(john).* matches => "+b); b=str.matches(".*(Hello i am john).*"); System.out.println("pattern: .*(Hello i am john).* matches => "+b); b=str.matches(".*(JOHN).*"); System.out.println("pattern: .*(JOHN).*matches => "+b); System.out.println(); String str1="Hello i am John Adam"; System.out.println(str1); b=str1.matches(".*(Adam).*"); //ignore after and before words System.out.println("pattern: .*(Adam).* matches => "+b); b=str.matches("John"); //just match for single word john System.out.println("pattern: John matches => "+b); } } |
Output:
===================
Number matching example
===================
pattern: .*[1-9].* matches => true
pattern: .*(100).* matches => false
pattern: .*(88).* matches => true
===================
Character matching example
===================
pattern: .*[A-Za-z].* matches => true
pattern: .*[A].* matches => false
pattern: .*[i].* matches => true
===================
Word and String matching example
===================
pattern: .*(john).* matches => true
pattern: .*(Hello i am john).* matches => true
pattern: .*(JOHN).*matches => falseHello i am johnMehra
pattern: .*(Mehra).* matches => true
pattern: john matches => false
That’s all about Java String’s matches method.