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

Quick Start

If you don't know regular expressions, please begin by going to the tutorial.

If you do know regular expressions, then there are just a few things you need to know to get going with regular expressions.

To create a regular expression that finds the pattern "abc.*def" you simply do one of these:

Regex r1 = Regex.perlCode("m/abc.*def/");
Regex r2 = new Regex("abc.*def");
To search for this pattern inside a String you can use any of these methods:
String s = "hello abc world def!";
if(r1.search(s)) {
  System.out.println("The pattern matched.");
  System.out.println("The matched substring: "+
    r1.stringMatched());
  System.out.println("The substring that preceeds the match: "+
    r1.left());
  System.out.println("The substring that follows the match: "+
    r1.right());
  System.out.println("The string matched from position="+
    r1.matchedFrom());
  System.out.println("The string matched to postion="+
    r1.matchedTo());
  System.out.println("# of characters matched="+
    r1.charsMatched());
} else
  System.out.println("The pattern did not match.");
If you are interested in getting at the backreference (the part in parenthesis) you can do it with these methods:
if(r1.didMatch()) {
  System.out.println("The pattern matched.");
  System.out.println("number of backrefs="+r1.numSubs());
  System.out.println("Backreference 1: "+
    r1.stringMatched(1));
  System.out.println("The substring that preceeds backref 1: "+
    r1.left(1));
  System.out.println("The substring that follows backref 1: "+
    r1.right(1));
  System.out.println("Backref 1 matched from position="+
    r1.matchedFrom(1));
  System.out.println("Backref 1 matched to postion="+
    r1.matchedTo(1));
  System.out.println("# of characters matched by backref 1="+
    r1.charsMatched(1));
} 
If you want to substitute, you can do this:
// Regex r3 and r4 are the same.
Regex r3 = new Regex("foo","bar");
Regex r4 = Regex.perlCode("s/foo/bar/");
To actually substitute the text of a string you just do this:
String res1 = r3.replaceFirst("foo and foo again");
// res1 contains "bar and foo again"
 
String res2 = r3.replaceAll("foo and foo again");
// res2 contains "bar and bar again"
You can use all those substitution patterns you're familiar with in the substitution rule, such as $&, $1, $', $`, etc.

Do you want your pattern to match faster? Just call the optimize() method.

r3.optimize();
This method will spend extra time trying to make your pattern execute faster.

Do you want to match a pattern but ignore case? Either of the standard methods you are familiar with will work.

// Both Regex's are the same.
Regex r5 = new Regex("(?i)foo");
Regex r6 = Regex.perlCode("m/foo/i");
Are you looking for syntax errors in your patterns?
Regex r7 = new Regex();
try {
  r7.compile("(foo");
} catch(RegSyntax rs) {
  System.err.println("Syntax error!");
}
That should get you started. Please look at the example snippets of code, and lastly the javadoc'd information to learn more. It may be, however, that what is presented in this quick start will allow you to do everything you are interested in. Happy computing!