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...

Things that have changed in version 1.5.3

Only bug fixing. This was a maintenance release.

Things that have changed in version 1.5.1

A serious bug was fixed. If two or three digits occurred in a pattern together this got converted to a single octal value, even if the sequence was not preceeded by a \. This was fixed. Also, the sequences \x, \s, etc. were not being interpreted when they occurred inside square brackets.

Things that have changed in version 1.5

When migrating from version 1.4, the biggest change to notice is that the x, m and s flags are now supported. If you don't know the signifcance of these flags, the m is explained here, and the s flag is explained here.

Previous versions of pat behaved as if the m flag was always on.

To recover the old behavior, you can make the static method call:

Previous versions of package pat did not implement the perl escape sequences \x, \c, \o, etc. The current version implements them all.

The RegexReader and RegexWriter have been significantly updated. The old design was quite clunky and did not work very well. The newer version can really do what you would like it to do -- replacing on the string as it reads.

There have also been a few bug fixes, not many. Please report any that you find.

Things that have changed in version 1.4

When migrating from previous versions of package pat, keep these things in mind:
  • The package name has changed from COM.stevesoft.pat to com.stevesoft.pat.
  • The class for writing your own replacement rules (see ReplaceRule) with java code has a new signature. The method apply(StringBuffer sb,RegRes rr) has become apply(StringBufferLike sbl,RegRes rr). The new method makes it possible for you to send the output of a replacement to other kinds of buffers than a StringBuffer.
  • If you are using custom patterns, the Validator now uses a StringLike instead of a String. See the javadoc page.
  • Previous versions of package pat relied on the compiler to recognize carriage returns. The new version accepts will recognize "\\n" as a carriage return.
  • The variables ignoreCase, and dontMatchInQuotes are no longer publically accessible. One can now set and get their values with methods such as getIgnoreCase() and setIgnoreCase().

New Features

  • Do you want to find the last occurrence of a pattern in a String? Try the new reverseSearch method of Regex.
  • There are new special unicode patterns. (??uc) matches upper case and (??lc) matches a lower case unicode character.
  • Package pat is now polymorphic. That means that anything conforming to a StringLike can be searched by Regex.

    Here are a few examples: We can search a file:
      import com.stevesoft.pat.wrap.*;
      // Retrieve a phone number from a RandomAccessFile
      public String getPhoneNumber(RandomAccessFile raf) {
        RandomAccessFileWrap rafw =
          new RandomAccessFileWrap(raf);
        Regex r =
          new Regex("\b\\(\\d{3}\\)\\d{3}-\\d{4}\b");
          return r.stringMatched();
          return null;
    Warning! This new found flexibility can help you to shoot yourself in the foot. Please check out this item in the faq.

    We can search a char[].
      import com.stevesoft.pat.wrap.*;
      // Retrieve a phone number from a char[].
      public String getSSN(char[] ca) {
        CharArrayWrap caw = new CharArrayWrap(ca);
        Regex r = new Regex("\b\\d{3}-\\d{2}-\\d{4}\b");
        // a social security number
          return r.stringMatched();
          return null;
    For more information, to see complete examples and to see how to redirect the result of a replace from a StringBuffer to some other object, see examples.html.