Regular Expressions in Java

Package com.stevesoft.pat version 1.5.3

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

Copyright/License
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
Examples
Support
FAQ
Documentation

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

Other stuff...
Phreida
xmlser

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+?");
r.search("36454");
System.out.println(r.stringMatched());
// Prints "3"
 
r = new Regex("\\d*?4");
r.search("36454");
System.out.println(r.stringMatched());
// 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("'.*'");
r.search("  'hello' and 'world'  ");
System.out.println(r.stringMatched());
// 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("'.*?'");
r.search("  'hello' and 'world' ");
System.out.println(r.stringMatched());
// 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");
r.search(" 'hello' and 'world' ");
System.out.println(r.stringMatched());
// Prints "'hello'"
 
r.search(" \"hello\" and \"world\" ");
System.out.println(r.stringMatched());
// 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";
System.out.println(r.search(x));
System.out.println(r.search(x));
System.out.println(r.search(x));
// 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