Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. 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.

Java String matches Example

In this example we are going to talk about matches String Class method. You can use this method to test a String against a regular expression. Testing a String 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 offers matches 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 checkout this 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 a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close