Home > Articles

  • Print
  • + Share This
This chapter is from the book

2.7 Regular Expressions

Regular expressions are used to specify string patterns. You can use regular expressions whenever you need to locate strings that match a particular pattern. For example, one of our sample programs locates all hyperlinks in an HTML file by looking for strings of the pattern <a href=". . .">.

Of course, when specifying a pattern, the . . . notation is not precise enough. You need to specify exactly what sequence of characters is a legal match, using a special syntax to describe a pattern.

In the following sections, we cover the regular expression syntax used by the Java API and discuss how to put regular expressions to work.

2.7.1 The Regular Expression Syntax

Let us start with a simple example. The regular expression


matches any string of the following form:

  • The first letter is a J or j.

  • The next three letters are ava.

  • The remainder of the string consists of one or more arbitrary characters.

For example, the string "javanese" matches this particular regular expression, but the string "Core Java" does not.

As you can see, you need to know a bit of syntax to understand the meaning of a regular expression. Fortunately, for most purposes, a few straightforward constructs are sufficient.

  • A character class is a set of character alternatives, enclosed in brackets, such as [Jj], [0-9], [A-Za-z], or [^0-9]. Here the - denotes a range (all characters whose Unicode values fall between the two bounds), and ^ denotes the complement (all characters except those specified).

  • To include a - inside a character class, make it the first or last item. To include a ], make it the first item. To include a ^, put it anywhere but the beginning. You only need to escape [ and \.

  • There are many predefined character classes such as \d (digits) or \p{Sc} (Unicode currency symbol). See Tables 2.6 and 2.7.

    Table 2.6 Regular Expression Syntax





    c, not one of . * + ? { | ( ) [ \ ^ $

    The character c



    Any character except line terminators, or any character if the DOTALL flag is set


    The Unicode code point with hex code p


    \uhhhh, \xhh, \0o, \0oo, \0ooo

    The UTF-16 code unit with the given hex or octal value


    \a, \e, \f, \n, \r, \t

    Alert (\x{7}), escape (\x{1B}), form feed (\x{B}), newline (\x{A}), carriage return (\x{D}), tab (\x{9})


    \cc, where c is in [A-Z] or one of @ [ \ ] ^ _ ?

    The control character corresponding to the character c

    \cH is a backspace (\x{8})

    \c, where c is not in [A-Za-z0-9]

    The character c


    \Q. . .\E

    Everything between the start and the end of the quotation

    \Q(. . .)\E matches the string (. . .)

    Character Classes

    [C1C2. . .], where Ci are characters, ranges c-d, or character classes

    Any of the characters represented by C1, C2, . . .


    [^. . .]

    Complement of a character class


    [. . .&&. . .]

    Intersection of character classes


    \p{. . .}, \P{. . .}

    A predefined character class (see Table 2.7); its complement

    \p{L} matches a Unicode letter, and so does \pL—you can omit braces around a single letter

    \d, \D

    Digits ([0-9], or \p{Digit} when the UNICODE_CHARACTER_CLASS flag is set); the complement

    \d+ is a sequence of digits

    \w, \W

    Word characters ([a-zA-Z0-9_], or Unicode word characters when the UNICODE_CHARACTER_CLASS flag is set); the complement

    \s, \S

    Spaces ([ \n\r\t\f\x{B}], or \p{IsWhite_Space} when the UNICODE_CHARACTER_CLASS flag is set); the complement

    \s*,\s* is a comma surrounded by optional white space

    \h, \v, \H, \V

    Horizontal whitespace, vertical whitespace, their complements

    Sequences and Alternatives


    Any string from X, followed by any string from Y

    [1-9][0-9]* is a positive number without leading zero


    Any string from X or Y




    Captures the match of X

    ’([^’]*)’ captures the quoted text


    The nth group

    ([’"]).*\1 matches ’Fred’ or "Fred" but not "Fred’


    Captures the match of X with the given name

    ’(?<id>[A-Za-z0-9]+)’ captures the match with name id


    The group with the given name

    \k<id> matches the group with name id


    Use parentheses without capturing X

    In (?:http|ftp)://(.*), the match after :// is \1

    (?f1f2. . .:X), (?f1. . .-fk. . .:X), with fi in [dimsuUx]

    Matches, but does not capture, X with the given flags on or off (after -)

    (?i:jpe?g) is a case-insensitive match

    Other (?. . .)

    See the Pattern API documentation



    Optional X

    \+? is an optional + sign

    X*, X+

    0 or more X, 1 or more X

    [1-9][0-9]+ is an integer ≥ 10

    X{n}, X{n,}, X{m,n}

    n times X, at least n times X, between m and n times X

    [0-7]{1,3} are one to three octal digits

    Q?, where Q is a quantified expression

    Reluctant quantifier, attempting the shortest match before trying longer matches

    .*(<.+?>).* captures the shortest sequence enclosed in angle brackets

    Q+, where Q is a quantified expression

    Possessive quantifier, taking the longest match without backtracking

    ’[^’]*+’ matches strings enclosed in single quotes and fails quickly on strings without a closing quote

    Boundary Matches

    ^, $

    Beginning, end of input (or beginning, end of line in multiline mode)

    ^Java$ matches the input or line Java

    \A, \Z, \z

    Beginning of input, end of input, absolute end of input (unchanged in multiline mode)

    \b, \B

    Word boundary, nonword boundary

    \bJava\b matches the word Java


    A Unicode line break


    The end of the previous match

    Table 2.7 Predefined Character Class Names Used with \p

    Character Class Name



    posixClass is one of Lower, Upper, Alpha, Digit, Alnum, Punct, Graph, Print, Cntrl, XDigit, Space, Blank, ASCII, interpreted as POSIX or Unicode class, depending on the UNICODE_CHARACTER_CLASS flag

    IsScript, sc=Script, script=Script

    A script accepted by Character.UnicodeScript.forName

    InBlock, blk=Block, block=Block

    A block accepted by Character.UnicodeBlock.forName

    Category, InCategory, gc=Category, general_category=Category

    A one- or two-letter name for a Unicode general category


    Property is one of Alphabetic, Ideographic, Letter, Lowercase, Uppercase, Titlecase, Punctuation, Control, White_Space, Digit, Hex_Digit, Join_Control, Noncharacter_Code_Point, Assigned


    Invokes the method Character.isMethod (must not be deprecated)

  • Most characters match themselves, such as the ava characters in the preceding example.

  • The . symbol matches any character (except possibly line terminators, depending on flag settings).

  • Use \ as an escape character. For example, \. matches a period and \\ matches a backslash.

  • ^ and $ match the beginning and end of a line, respectively.

  • If X and Y are regular expressions, then XY means “any match for X followed by a match for Y.” X | Y means “any match for X or Y.”

  • You can apply quantifiers X+ (1 or more), X* (0 or more), and X? (0 or 1) to an expression X.

  • By default, a quantifier matches the largest possible repetition that makes the overall match succeed. You can modify that behavior with suffixes ? (reluctant, or stingy, match: match the smallest repetition count) and + (possessive, or greedy, match: match the largest count even if that makes the overall match fail).

    For example, the string cab matches [a-z]*ab but not [a-z]*+ab. In the first case, the expression [a-z]* only matches the character c, so that the characters ab match the remainder of the pattern. But the greedy version [a-z]*+ matches the characters cab, leaving the remainder of the pattern unmatched.

  • You can use groups to define subexpressions. Enclose the groups in ( ), for example, ([+-]?)([0-9]+). You can then ask the pattern matcher to return the match of each group or to refer back to a group with \n where n is the group number, starting with \1.

For example, here is a somewhat complex but potentially useful regular expression that describes decimal or hexadecimal integers:


Unfortunately, the regular expression syntax is not completely standardized between various programs and libraries; there is a consensus on the basic constructs but many maddening differences in the details. The Java regular expression classes use a syntax that is similar to, but not quite the same as, the one used in the Perl language. Table 2.6 shows all constructs of the Java syntax. For more information on the regular expression syntax, consult the API documentation for the Pattern class or the book Mastering Regular Expressions by Jeffrey E. F. Friedl (O’Reilly and Associates, 2006).

2.7.2 Matching a String

The simplest use for a regular expression is to test whether a particular string matches it. Here is how you program that test in Java. First, construct a Pattern object from a string containing the regular expression. Then, get a Matcher object from the pattern and call its matches method:

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) . . .

The input of the matcher is an object of any class that implements the CharSequence interface, such as a String, StringBuilder, or CharBuffer.

When compiling the pattern, you can set one or more flags, for example:

Pattern pattern = Pattern.compile(expression,

Or you can specify them inside the pattern:

String regex = "(?iU:expression)";

Here are the flags:

  • Pattern.CASE_INSENSITIVE or i: Match characters independently of the letter case. By default, this flag takes only US ASCII characters into account.

  • Pattern.UNICODE_CASE or u: When used in combination with CASE_INSENSITIVE, use Unicode letter case for matching.

  • Pattern.UNICODE_CHARACTER_CLASS or U: Select Unicode character classes instead of POSIX. Implies UNICODE_CASE.

  • Pattern.MULTILINE or m: Make ^ and $ match the beginning and end of a line, not the entire input.

  • Pattern.UNIX_LINES or d: Only ’\n’ is a line terminator when matching ^ and $ in multiline mode.

  • Pattern.DOTALL or s: Make the . symbol match all characters, including line terminators.

  • Pattern.COMMENTS or x: Whitespace and comments (from # to the end of a line) are ignored.

  • Pattern.LITERAL: The pattern is taken literally and must be matched exactly, except possibly for letter case.

  • Pattern.CANON_EQ: Take canonical equivalence of Unicode characters into account. For example, u followed by ¨ (diaeresis) matches ü.

The last two flags cannot be specified inside a regular expression.

If you want to match elements in a collection or stream, turn the pattern into a predicate:

Stream<String> strings = . . .;
Stream<String> result = strings.filter(pattern.asPredicate());

The result contains all strings that match the regular expression.

If the regular expression contains groups, the Matcher object can reveal the group boundaries. The methods

int start(int groupIndex)
int end(int groupIndex)

yield the starting index and the past-the-end index of a particular group.

You can simply extract the matched string by calling

String group(int groupIndex)

Group 0 is the entire input; the group index for the first actual group is 1. Call the groupCount method to get the total group count. For named groups, use the methods

int start(String groupName)
int end(String groupName)
String group(String groupName)

Nested groups are ordered by the opening parentheses. For example, given the pattern


and the input


the matcher reports the following groups

Group Index




















Listing 2.6 prompts for a pattern, then for strings to match. It prints out whether or not the input matches the pattern. If the input matches and the pattern contains groups, the program prints the group boundaries as parentheses, for example:


Listing 2.6 regex/RegexTest.java

 1  package regex;
 3  import java.util.*;
 4  import java.util.regex.*;
 6  /**
 7  * This program tests regular expression matching. Enter a pattern and strings to match,
 8  * or hit Cancel to exit. If the pattern contains groups, the group boundaries are displayed
 9  * in the match.
10  * @version 1.03 2018-05-01
11  * @author Cay Horstmann
12  */
13  public class RegexTest
14  {
15    public static void main(String[] args) throws PatternSyntaxException
16    {
17        var in = new Scanner(System.in);
18        System.out.println("Enter pattern: ");
19        String patternString = in.nextLine();
21        Pattern pattern = Pattern.compile(patternString);
23        while (true)
24        {
25          System.out.println("Enter string to match: ");
26          String input = in.nextLine();
27          if (input == null || input.equals("")) return;
28          Matcher matcher = pattern.matcher(input);
29          if (matcher.matches())
30          {
31              System.out.println("Match");
32              int g = matcher.groupCount();
33              if (g > 0)
34              {
35                for (int i = 0; i < input.length(); i++)
36                {
37                    // Print any empty groups
38                    for (int j = 1; j <= g; j++)
39                      if (i == matcher.start(j) && i == matcher.end(j))
40                          System.out.print("()");
41                    // Print ( for non-empty groups starting here
42                    for (int j = 1; j <= g; j++)
43                      if (i == matcher.start(j) && i != matcher.end(j))
44                          System.out.print(’(’);
45                    System.out.print(input.charAt(i));
46                    // Print ) for non-empty groups ending here
47                    for (int j = 1; j <= g; j++)
48                      if (i + 1 != matcher.start(j) && i + 1 == matcher.end(j))
49                          System.out.print(’)’);
50                }
51                System.out.println();
52              }
53            }
54            else
55                System.out.println("No match");
56          }
57      }
58  }

2.7.3 Finding Multiple Matches

Usually, you don’t want to match the entire input against a regular expression, but to find one or more matching substrings in the input. Use the find method of the Matcher class to find the next match. If it returns true, use the start and end methods to find the extent of the match or the group method without an argument to get the matched string.

while (matcher.find())
   int start = matcher.start();
   int end = matcher.end();
   String match = input.group();
   . . .

In this way, you can process each match in turn. As shown in the code fragment, you can get the matched string as well as its position in the input string.

More elegantly, you can call the results method to get a Stream<MatchResult>. The MatchResult interface has methods group, start, and end, just like Matcher. (In fact, the Matcher class implements this interface.) Here is how you get a list of all matches:

List<String> matches = pattern.matcher(input)

If you have the data in a file, you can use the Scanner.findAll method to get a Stream<MatchResult>, without first having to read the contents into a string. You can pass a Pattern or a pattern string:

var in = new Scanner(path, StandardCharsets.UTF_8);
Stream<String> words = in.findAll("\\pL+")

Listing 2.7 puts this mechanism to work. It locates all hypertext references in a web page and prints them. To run the program, supply a URL on the command line, such as

java match.HrefMatch http://horstmann.com

Listing 2.7 match/HrefMatch.java

 1  package match;
 3  import java.io.*;
 4  import java.net.*;
 5  import java.nio.charset.*;
 6  import java.util.regex.*;
 8  /**
 9  * This program displays all URLs in a web page by matching a regular expression that
10  * describes the <a href=. . .> HTML tag. Start the program as <br>
11  * java match.HrefMatch URL
12  * @version 1.03 2018-03-19
13  * @author Cay Horstmann
14  */
15  public class HrefMatch
16  {
17    public static void main(String[] args)
18    {
19        try
20        {
21          // get URL string from command line or use default
22          String urlString;
23          if (args.length > 0) urlString = args[0];
24          else urlString = "http://openjdk.java.net/";
26          // read contents of URL
27          InputStream in = new URL(urlString).openStream();
28          var input = new String(in.readAllBytes(), StandardCharsets.UTF_8);
30          // search for all occurrences of pattern
31          var patternString = "<a\\s+href\\s*=\\s*(\"[^\"]*\"|[^\\s>]*)\\s*>";
32          Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
33          pattern.matcher(input)
34              .results()
35              .map(MatchResult::group)
36              .forEach(System.out::println);
37        }
38        catch (IOException | PatternSyntaxException e)
39        {
40            e.printStackTrace();
41        }
42      }
43  }

2.7.4 Splitting along Delimiters

Sometimes, you want to break an input along matched delimiters and keep everything else. The Pattern.split method automates this task. You obtain an array of strings, with the delimiters removed:

String input = . . .;
Pattern commas = Pattern.compile("\\s*,\\s*");
String[] tokens = commas.split(input);
   // "1, 2, 3" turns into ["1", "2", "3"]

If there are many tokens, you can fetch them lazily:

Stream<String> tokens = commas.splitAsStream(input);

If you don’t care about precompiling the pattern or lazy fetching, you can just use the String.split method:

String[] tokens = input.split("\\s*,\\s*");

If the input is in a file, use a scanner:

var in = new Scanner(path, StandardCharsets.UTF_8);
Stream<String> tokens = in.tokens();

2.7.5 Replacing Matches

The replaceAll method of the Matcher class replaces all occurrences of a regular expression with a replacement string. For example, the following instructions replace all sequences of digits with a # character:

Pattern pattern = Pattern.compile("[0-9]+");
Matcher matcher = pattern.matcher(input);
String output = matcher.replaceAll("#");

The replacement string can contain references to the groups in the pattern: $n is replaced with the nth group, and ${name} is replaced with the group that has the given name. Use \$ to include a $ character in the replacement text.

If you have a string that may contain $ and \, and you don’t want them to be interpreted as group replacements, call matcher.replaceAll(Matcher.quoteReplacement(str)).

If you want to carry out a more complex operation than splicing in group matches, you can provide a replacement function instead of a replacement string. The function accepts a MatchResult and yields a string. For example, here we replace all words with at least four letters with their uppercase version:

String result = Pattern.compile("\\pL{4,}")
   .matcher("Mary had a little lamb")
   .replaceAll(m -> m.group().toUpperCase());
   // Yields "MARY had a LITTLE LAMB"

The replaceFirst method replaces only the first occurrence of the pattern.

You have now seen how to carry out input and output operations in Java, and had an overview of the regular expression package that was a part of the “new I/O” specification. In the next chapter, we turn to the processing of XML data.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information

To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.


Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.


If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information

Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.


This site is not directed to children under the age of 13.


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information

If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information

Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents

California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure

Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact

Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice

We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020