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:

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:

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.

Was this post helpful?

Related Posts

  • 15 September

    How to Replace Space with Underscore in Java

    Table of ContentsReplace space with underscore in java1. Using replace() method2. Using replaceAll() method Learn about how to replace space with underscore in java. Replace space with underscore in java 1. Using replace() method Use String’s replace() method to replace space with underscore in java. String’s replace() method returns a string replacing all the CharSequence […]

  • 15 September

    How to Replace Comma with Space in Java

    Table of ContentsReplace comma with space in java1. Using replace() method2. Using replaceAll() method Learn about how to replace comma with space in java. Replace comma with space in java 1. Using replace() method Use String’s replace() method to replace comma with space in java. Here is syntax of replace() method: [crayon-633185e02ca79717940981/] [crayon-633185e02ca7f647372603/] Output: 1 […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusion Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text data at contiguous […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in Java In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix of format specifier. […]

  • Find character in String in java
    02 February

    Find Character in String in Java

    Table of ContentsHow to Find Character in String in Java1. Using indexOf() Method to Find Character in String in Java2. Using lastIndexOf() Method to Find Char in String in JavaFind Character at Index in String in Javahow Do I Find Word in String in Java? In this post, we will see how to find character […]

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusion In this […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.