Regular Expressions in Java

Package com.stevesoft.pat version 1.5.3

Mugs, T-shirts Comments/Raves
New in 1.5.3
A Game
An Online Test

Download Free

 If you need a non-LGPL version
You Can Buy!

Online help...
Quick Start
Tutorial Part 1
Tutorial Part 2
Tutorial Part 3
Tutorial Part 4
Tutorial Part 5
Tutorial Part 6

Useful apps...
Java Beautifier
Code Colorizer
GUI Grep
Swing Grep

Other stuff...

Tutorial Part 4

Pattern Elements

{1,}?, (?#), \1, \2, \G

We only have a few pattern elements left to explore before the syntax of Perl 5 is covered. The first is to follow a pattern element such as {2,}, {3,8}, +, *, ? by a question mark. The effect of this is to make the pattern matching less hungry, instead of matching the most times it can the pattern matcher will attempt to match the fewest number of times it can.
Regex r = new Regex("\\d+?");"36454");
// Prints "3"
r = new Regex("\\d*?4");"36454");
// Prints "364"
Now suppose you want to match the text between single quotes. You would want to use a minimumal match, not a maximal.
Regex r = new Regex("'.*'");"  'hello' and 'world'  ");
// Prints "'hello' and 'world'"
// This isn't really what we wanted, we got
// the text inside two sets of ''s as well as
// some from in between.
r = new Regex("'.*?'");"  'hello' and 'world' ");
// Prints 'hello'
But let's get a little more fancy. Suppose we want to match the text between either single or double quotes. We can do this as follows
Regex r = new Regex("(['\"]).*?\\1");" 'hello' and 'world' ");
// Prints "'hello'"" \"hello\" and \"world\" ");
// Prints "\"hello\""
The "\\1" matches the text contained in the first backreference. You can likewise use "\\2" to match the second backreference (if you have a second backreference).

If you wish one search to pick up where the last one left off you can use the "\G" pattern element. If the string hasn't been searched before, then "\G" matches the beginning of the String.

Regex r = new Regex("\\Gfoo");
String x = "foofoo foo";
// Prints true, true, false.

Finally, you can add comments inside your patterns using the "(?#)" notation. This last pattern is the same as the previous one, it just has a comment added. It works exactly the same, however.

Regex r = new Regex("(['\"]).*?\\1(?# I like this pattern)");
This completes the set of pattern elements that exist in perl 5.

For a pattern summary table click here.

Previous Next