Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Java Regex Tutorial

Lars Vogel

Version 0.6

07.12.2009

Revision History
Revision 0.107.12.2007Lars Vogel
Created list of regex expressions
Revision 0.230.12.2008Lars Vogel
Added Java usage description
Revision 0.303.01.2009Lars Vogel
Re-structured article
Revision 0.421.07.2009Lars Vogel
Clean-up
Revision 0.506.12.2009Lars Vogel
Improved description of qualifiers
Revision 0.607.12.2009Lars Vogel
Re-structured the article

Java and Regular Expressions

This article gives an overview of the usage of regular expressions in general and describes the usage of regular expressions with Java. It also provides several Java regular expression examples.


Table of Contents

1. Regular Expressions
1.1. Overview
1.2. Usage
1.3. Junit
2. Regular Expressions
2.1. Common matching symbols
2.2. Metacharacters
2.3. Quantifier
3. Using Regular Expressions with String.matches()
3.1. Overview
3.2. Examples
4. Pattern and Matcher
5. Java Regex Examples
5.1. Or
5.2. Phone number
5.3. Check for a certain number range
5.4. Building a link checker
6. Thank you
7. Questions and Discussion
8. Links and Literature

1. Regular Expressions

1.1.  Overview

A regular expression define a search pattern for strings. This pattern may match one or several times or not at all for a given string. The abbreviation for regular expression is "regex".

A simple example for a regular expression is a (literal) string. For example the regex "Hello World" will match exactly the phrase "Hello World". Another example for a regular expression is "." (dot) which matches any single character; it would match for example "a" or "z" or "1".

1.2.  Usage

Regular expressions can be used to search, edit and manipulate text.

Regular expressions are used in several programming languages, e.g. Java but also Perl, Groovy, etc. Unfortunately each language / program supports regex slightly different.

The pattern defined by the regular expression is applied on the string from left to right. Once a source character has been used in a match, it cannot be reused. For example the regex "aba" will match "ababababa" only two times (aba_aba__).

1.3.  Junit

Some of the following examples use JUnit to validate the result. You should be able to adjust them in case if you don't want to use JUnit. To learn about JUnit please see JUnit Tutorial .