Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems

Java Regular Expressions
Up to and including Java2 release 1.3, the only regular expression handlers available were third party supplied. As from Java2 1.4, a regex class java.util.regex was added to the main Java distribution, and it's that regular expression handler that we provide information on on this page.

Operator TypeExamplesDescription
Literal Characters
Match a character exactly
a A y 6 % @Letters, digits and many special
characters match exactly
\$ \^ \+ \\ \?Precede other special characters
with a \ to cancel their regex special meaning
\n \t \rLiteral new line, tab, return
\cJ \cGControl codes
\xa3Hex codes for any character
Anchors and assertions ^Starts with
$Ends with
\b \Bon a word boundary,
NOT on a word boundary
Character groups
any 1 character from the group
[aAeEiou]any character listed from [ to ]
[^aAeEiou]any character except aAeEio or u
[a-fA-F0-9]any hex character (0 to 9 or a to f)
.any character at all
(not new line in some circumstances)
\sany space character (space \n \r or \t)
\wany word character (letter digit or _)
\dany digit (0 through 9)
\S \W \Dany character that is NOT a space
word character or digit
apply to previous element
+1 or more ("some")
*0 or more ("perhaps some")
?0 or 1 ("perhaps a")
{4}exactly 4
{4,}4 or more
{4,8}between 4 and 8
Add a ? after any count to turn it sparse (match as few as possible) rather than have it default to greedy
Alternation |either, or
Grouping ( )group for count and save to variable
(?: )group for count but do not save
Variables $xyzInsert contents of $xyz into regular expression
\1 \2Back reference to 1st, 2nd etc matched groups

The above list shows the most commonly used elements of Java regular expressions. It is not exhaustive.

In order to use a Regular Expression in Java 1.4 a regular expression, specified as a String, must first be compiled into an instance of the class Pattern. The resulting pattern can then be used to create a Matcher object that can match arbitrary character i sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.

A typical invocation sequence is thus
   Pattern p = Pattern.compile("a*b");
   Matcher m = p.matcher("aaaaab");
   boolean b = m.matches();

Back to Regular Expression Home Page
Jump to Elements of a regular expression
Order a Regular Expression Mousemat for £4.95 inclusive

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho