Java String matches example

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:

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:

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:

Hello i am john,id is 1 and my seat number is 88
===================
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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.