Java Regular Expressions


What is a Regular Expression?

A common expression is a sequence of letters that form a search pattern. When searching for data in text, you can use this search pattern to define search terms.

A common expression can be a single letter, or a complex pattern.

Ordinary expressions can be used to perform all types of text searches and to replace text.

Java does not have a built-in Regular Expression section, but we can import java.util.regex package to work with common expressions. The package includes the following classes:

  • Pattern Class - Defines a pattern (to be used in search)
  • Matcher Class - Used for pattern search
  • PatternSyntaxException Class - Displays syntax error in normal speech pattern

Example

Find out if there are any occurrences of the word "w3schools" in a sentence:

import java.util.regex.Matcher;
            import java.util.regex.Pattern;
            
            public class Main {
              public static void main(String[] args) {
                Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher("Visit W3Schools!");
                boolean matchFound = matcher.find();
                if(matchFound) {
                  System.out.println("Match found");
                } else {
                  System.out.println("Match not found");
                }
              }
            }
            // Outputs Match found
            

Example Explained

In this example, the word "w3schools" is searched in a sentence.

First, the pattern was created using the Pattern.compile() method. The first parameter indicates which pattern is being searched and the second parameter has a flag indicating that the search should not be sensitive. The second parameter is voluntary.

The matcher() method is used to search for a pattern in a character unit. Returns a Matcher item containing information about the search performed.

The find() method returns true if a pattern is found in a character unit and is false if not found.


Flags

Flags in compile() change the way search is performed. Here are a few of them:

  • Pattern.CASE_INSENSITIVE - Character case will be ignored when searching.
  • Pattern.LITERAL - Special characters in the pattern will not have a special meaning and will be treated as normal characters when searching.
  • Pattern.UNICODE_CASE - Use it with the CASE_INSENSITIVE flag to ignores the story of characters outside of English characters

Regular Expression Patterns

The first parameter of the Pattern.compile() pattern. Defines what you are looking for.

Brackets are used to get the list of characters:


Expression Description
[abc] Find one character from the options between the brackets
[^abc] Find one character NOT between the brackets
[0-9] Find one character from the range 0 to 9

Metacharacters

Metacharacts are characters with a special meaning:


Metacharacter Description
| Find a match for any one of the patterns separated by | as in: cat|dog|fish
. Find just one instance of any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning of a word like this: \bWORD, or at the end of a word like this: WORD\b
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx

Quantifiers

Quantifiers quote prices:


Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n
n{x} Matches any string that contains a sequence of X n's
n{x,y} Matches any string that contains a sequence of X to Y n's
n{x,} Matches any string that contains a sequence of at least X n's

Note: If your quote needs to search for one of the special characters you can use backslash (\) to escape. In Java, cable backslash must be run automatically, so two backslash is required to avoid special characters. For example, to search for one or more question marks you could use the following expression: "\\?"