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


  1. The java compiler can't find com.stevesoft.pat.Regex:
    1. Did you purchase from GetSoftware? If so, you received a file called You should unzip this file and obtain two files: README.html and patbin15_118.jar. If you don't get this result, try a different unzip program.

      Recommended methods of unzipping:

      • jar xvf
      • unzip (using InfoZip's unzip utility version 5.32 or greater)
      • WinZip version 6.3 or later
      • PkZip version 2.5 or later
    2. Did you set your CLASSPATH to include patbin15_118.jar.
    3. If that didn't work, perhaps the permissions are set wrong (if you are on a UNIX system). Make sure patbin15_118.jar can be read.
    4. If all of these fail, you may still get java to find the Regex by unzipping patbin15_118.jar. For example, you unzip your package so that Regex is in /myclasses/com/stevesoft/pat/Regex.class. If you now set the CLASSPATH to include the directory /myclasses, everything should work.
    5. Is your computer switched on? (just kidding).
  2. The pattern "(a)*" when matched against "aaa" contains only "a": This is not a bug. The backreference can only match a single "a" since that is all that's in the parenthesis. If you wanted to match all three, you should've used the pattern "(a*)".
  3. The code
    Regex r = new Regex("s/X/Y/");
    just prints out "XXX" and the replacement does not occur:

    The answer is simple, you really meant to do this:

    Regex r = Regex.perlCode("s/X/Y/");
    or this
    // The first argument to Regex() is the pattern,
    // the second is the replacement rule.
    Regex r = new Regex("X","Y");
  4. Why does my match take so long to complete? I find that people frequently use patterns with multiple ".*" or multiple ".*?" sub-patterns in them. This sort of thing is harmless unless you are searching a really large string. If you have a String with 3K characters and you have four ".*" elements you might have to search 3,000 to the 4th power or about 81,000,000,000,000 possible matches just to find your result. In that case, it is worthwhile to be careful in selecting what you are trying to match. Maybe "." isn't the right thing, maybe "[^<]" is more appropriate, or maybe "['\"]" is what you want.
  5. Some other problem: Are you using the right binary? We have versions available that are compiled with java 1.0.2, 1.1.8, 1.2.2, and 1.3. Sometimes this makes a difference.
  6. Some other problem: Send email. We will help you get things working, and We will not bomb you with email or give your address to anyone.