String

Java String matches Example

Photo of Nikos MaravitsasNikos MaravitsasMarch 19th, 2014Last Updated: March 19th, 2014
0 195 1 minute read

In this example we are going to talk aboutmatchesString Class method. You can use this method to test aString against a regular expression. Testing aString against a regular expression is a very common operation for interactive applications, as it is heavily used to perform validity checks on user input. It can also be used for several other causes on a bigger scale, like filtering, pruning on large text data, searching text documents etc.

So as you might imagine, Java offersmatches as a very simple API method to test Strings against regular expressions.

Let’s see some examples:

  • Check if a sentence has only letters : [a-zA-Z *]+$

StringMacthesExample.java:

package com.javacodegeeks.core.string;public class StringMacthesExample {public static void main(String[] args) {String s1 = "Java Code Geeks are awesome";System.out.println(s1.matches("[a-zA-Z *]+$"));}}

Output:

true
  • Check if a sentence has only alphanumerics : [a-zA-Z0-9 *]+$

StringMacthesExample.java:

package com.javacodegeeks.core.string;public class StringMacthesExample {public static void main(String[] args) {String s1 = "Java 1 Code 2 Geeks 3 are awesome 15675";System.out.println(s1.matches("[a-zA-Z0-9 *]+$"));}}

Output:

true
  • Check if a string is an alphanumeric word: \w*$

StringMacthesExample.java:

package com.javacodegeeks.core.string;public class StringMacthesExample {public static void main(String[] args) {String s1 = "Java8Rocks";System.out.println(s1.matches("\\w*$"));}}

Output:

true
  • Check if a string is a valid email address:^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$

StringMacthesExample.java:

package com.javacodegeeks.core.string;public class StringMacthesExample {public static void main(String[] args) {System.out.println("james.harlem@javacodegeeks.com".matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*"+ "(\\.[A-Za-z]{2,})$"));}}

Output:

true

So there you go. To learn more about Regular expression syntax you can checkoutthis site.

Download the Source Code

This was a Java String matches Example. You can download the source code of this example here : StringMatchesExample.zip

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Nikos MaravitsasNikos MaravitsasMarch 19th, 2014Last Updated: March 19th, 2014
0 195 1 minute read
Photo of Nikos Maravitsas

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.