Basic Java Regular Expressions

Today I will show you the very basics of Regular Expressions and how to use them in Java.

This won’t be a extensive and detailed Regular Expressions tutorial. Regular Expressions are huge topic on their own. I will show you only the basics, so you can quickly start to use regular expressions in your projects. No long explanations, no hastle… only a few examples and you are ready to go.

regular expressions java

Regular Expressions Methods in String Class

The String class in Java 8 has a few methods using regular expressions (or regex).  Here is a list of this methods. For more details you can check the String javadoc here.

  • boolean String.matches(String regex) – Tells whether or not this string matches the given regular expression
  • String replaceAll(String regex, String replacement) – Replaces each substring of this string that matches the given regular expression with the given replacement
  • String replaceFirst(String regex, String replacement) – Replaces the first substring of this string that matches the given regular expression with the given replacement
  • String[] split(String regex) – Splits this string around matches of the given regular expression
  • String[] split(String regex, int limit) – Splits this string around matches of the given regular expression. The pattern is applied “limit” number of times




Match a Char

Let’s assume we have following String: “Hello, it’s me” and we want to split the words in it by whitespace.

String str = "Hello, it's me";
String[] words = str.split(" ");
for (String word : words) {
	System.out.println(word);
}

this will produce following output

Hello,
it's
me

But what if we put two whitespaces between “it’s” and “me” like this: “Hello, it’s  me” ?

The output of our program will be:

Hello,
it's

me

So how can we avoid this? Keep reading and you will find out 🙂

Repetition

The + sign means match 1 or more in the same row.

Lets go back to our previous example “Hello, it’s  me” with two whitespaces between “it’s” and “me”.

In our code we will change the pattern to ” +”. In this case we will match against 1 or more whitespaces in a row

String str = "Hello, it's  me";
String[] words = str.split(" +");
for (String word : words) {
	System.out.println(word);
}

and the output is:

Hello,
it's
me

Concatenation

With regular expressions we can match a string or multiple chars in a row giving this as a regex pattern. For example the pattern “ing” will match like this:

“Earning money is easy as counting 1 2 33. Nah!”

In other words if “ing” is provided in our string, the string matches.

Combine Concatenation and Repetition

Pattern: “ar+” will match

arrows are not as fast as bullets”

Match zero or more

Pattern: “it*” will match

it‘s fun to learn just sitting in front of my computer”

The * (asterisk sign) will match “i” followed by zero or more “t”. In our example it will match “it”, “itt”, “i”, “i”

Alternation

Pattern: “ea|in” will match

earning money is easy as counting 1 2 33. Nah!”

The | (vertical bar sign) will match either “ea” or “in”.

Character Classes

Pattern: “[123]” will match

“Earning money is easy as counting 1 2 33. Nah!

[] matches any character in the set

Match Ranges

Pattern: “[1-3]” will match

“Earning money is easy as counting 1 2 33. Nah!

Pattern: “[a-f]” will match

“Earning money is easy as counting 1 2 33. Nah!

Excluding a Character

Pattern: “[^a-z123 ]” will match

Earning money is easy as counting 1 2 33. Nah!

^ indicates NOT any character in this set

Validate Email Address Regular Expression

I will give you an example how to validate a email address in Java. This is more complex but commonly used regex pattern:

^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;

and implementation:

String email = "info@javatutorial.net";
if (email.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;")) {
	System.out.println(email + "is a valid e-mail address");
} else {
	System.out.println(email + "is invalid e-mail address");
}

 

0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments