Java - Checking string patterns with String.matches() and introducing various examples

You can check if it contains a string of a specific pattern with String.matches(). A specific pattern refers to a regular expression (Regex). Returns a boolean indicating whether the regular expression matches the string. This lets me know if the string consists of the pattern I`m looking for.

Lets see how to apply matches()` by introducing various patterns as examples.

matches() basic example

matches() takes a regular expression as an argument. And it returns whether the string matches the pattern.

There doesnt seem to be much difference from String.contains()`, but if you look at the following example, there is a difference.

String str = "Welcome to codechacha";

System.out.print("Does String contains regex (.*)code(.*) ? : ");
System.out.println(str.matches("(.*)code(.*)"));

System.out.print("Does String contains regex code ? : ");
System.out.println(str.matches("code"));

System.out.print("Does String contains regex Welcome(.*) ? : " );
System.out.println(str.matches("Welcome(.*)"));

In the example above, (.*) means any string of zero or more characters. Thats why the regular expression (.)code(.)only checks if code exists, regardless of any strings on either side of the code. You can think of this regular expression as almost equivalent tocontains("code")`.

The regular expression code, on the other hand, determines that it does not match if it has any characters on either side. That`s why it returns false.

Welcome(.*) returns true when there is a pattern that starts with Welcome followed by zero or more characters.

Output:

Does String contains regex (.*)code(.*) ? : true
Does String contains regex code ? : false
Does String contains regex Welcome(.*) ? : true

Difference between contains() and matches()

As explained above, contains() simply returns whether the string passed as an argument exists. On the other hand, matches() takes a regular expression as an argument and returns true if it is a string with the same pattern.

Since regular expressions also take into account characters surrounding the character they are looking for, it can be difficult to apply if you don`t know regular expressions in detail.

System.out.print("Does String contains regex (.*)code(.*) ? : ");
System.out.println(str.matches("(.*)code(.*)"));

System.out.print("Does String contains regex code ? : ");
System.out.println(str.matches("code"));

find true/true string pattern

When we receive the string true from the user, we need to check whether the string is actually true. Of course, you can either use the if statement twice or use contains to be case sensitive. However, in this article, we will learn about matches, so we implemented it using matches.

Implemented with matches() as follows.

String str = "True";
System.out.println("Is it true ? : " + str.matches("[Tt]rue"));

str = "true";
System.out.println("Is it true ? : " + str.matches("[Tt]rue"));

str = "yes";
System.out.println("Is it true ? : " + str.matches("[Tt]rue"));

Output:

Is it true ? : true
Is it true ? : true
Is it true ? : false

The regular expression [Tt] matches either "T" or "t" character.

Find True, Yes string pattern

The following example finds the True/true and Yes/yes strings together.

String str = "yes";
System.out.println("Is it true or yes ? : " + str.matches("[Tt]rue|[Yy]es"));

str = "true";
System.out.println("Is it true or yes ? : " + str.matches("[Tt]rue|[Yy]es"));

str = "no";
System.out.println("Is it true or yes ? : " + str.matches("[Tt]rue|[Yy]es"));

In regular expressions, | means or.

Output:

Is it true or yes ? : true
Is it true or yes ? : true
Is it true or yes ? : false

Find letter and number patterns

The following example finds patterns with zero or more a and zero or more numbers.

String str = "aaa123";
System.out.println(str.matches("a*[0-9]*"));

Output:

true

find string pattern

The following starts with "H", followed by any letter, and finally ends with "llo" looking for a pattern.

System.out.println("Hello".matches("H.llo"));
System.out.println("Hallo".matches("H.llo"));
System.out.println("Hollo".matches("H.llo"));

Output:

true
true
true

In regular expressions, . means any single character. So it just needs to be some 1 letter after H.

Find patterns that start with EX

The following looks for a pattern that starts with "EX_" followed by a letter from a to g or a number from 1 to 5.

System.out.println("Ex_a".matches("Ex_[a-g1-5]"));
System.out.println("Ex_g".matches("Ex_[a-g1-5]"));
System.out.println("Ex_1".matches("Ex_[a-g1-5]"));
System.out.println("Ex_6".matches("Ex_[a-g1-5]"));

Output:

true
true
true
false

In regular expressions, ‘ag’ means one letter from a to g. And, ‘1-5’ means one number from 1 to 5.

Patterns that start with This and end with .

The following looks for patterns that start with This and end with a "." If it doesn`t end with ".", it returns false.

System.out.println("This is the right decision.".matches("This.*\\."));
System.out.println("This is me".matches("This.*\\."));
System.out.println("This is my mistake.".matches("This.*\\."));

Output:

true
false
true

If you want to express simply the character . rather than the regular expression ., you must write \\..

Find It + a few patterns

Look for patterns that start with It and have a few in the middle.

System.out.println("It's been a few days".matches("It.+a few.*"));
System.out.println("It's been a few over a year".matches("It.+a few.*"));
System.out.println("It's been about a week".matches("It.+a few.*"));

Output:

true
true
false

In regular expressions, .+ means one or more characters.

[A-1] Find pattern

The following looks for patterns that start with "[A-Number]".

System.out.println("[A-1] Jason".matches("\\[A\\-.+\\].*"));
System.out.println("[A-12] Todd".matches("\\[A\\-.+\\].*"));
System.out.println("[B-12] MJ".matches("\\[A\\-.+\\].*"));

Output:

true
true
false

If you want to use [ as the meaning of a simple character in a regular expression, you must type it as \\[. In case of -, it must be entered as \\-.

How to apply matches with ## Pattern You can apply matches using the Pattern class rather than String.matches().

If you pass regex and string as arguments to Pattern.matches() as follows, the same result as String.matches() is returned.

String text = "The URL is: http://mydomain.com";
String regex = ".*http://.*";

boolean matches = Pattern.matches(regex, text);
System.out.println(matches);

Reference

codechachaCopyright ©2019 codechacha