Java Regular Expressions (java.util.regex)
Java has a powerful regular expression API in the
java.util.regex package. This tutorial will
explain how to use it.
A regular expression is a textual pattern used to search in text. In other words, the text is searched for occurrences of the pattern. For instance, you could use a regular expression to search through a web page to scan for email addresses, or URL's.
Regular Expression Example
Here is a simple java regular expression example that searches a text for occurrences of
String text = "This is the text to be searched " + "for occurrences of the http:// pattern."; String pattern = ".*http://.*"; boolean matches = Pattern.matches(pattern, text); System.out.println("matches = " + matches);
The example does not actually check if the found
http:// string is part of a valid
URL, with domain name and suffix (.com, .net etc.). It just searches for occurrences of the
Java 6 Regular Expression API
The version of Java's regular expression API that is covered by this tutorial is the version found in Java 6.
Table of Contents
Here is a list of the topics covered in this Java regular expression. This list (menu) is also present at the top right of every page in the trail.
|Java Language Tutorial|
|Java Regex: Introduction|
|Java Regex: Pattern|
|Java Regex: Matcher|
|Java Regex: Syntax|
Feel Free to Contact Me
If you disagree with anything I write here in this Java regular expression tutorial, or just have comments, questions, etc. to
java.util.regex package, feel free to send
me an email. You wouldn't be the first to do so.
You can find my email address on the about page.