The Source for Java Technology Collaboration


Home | Help | Changes | Index | Search | Go

RegularExpressions

Regular expressions are common in many programming languages and Unixish utilities and provide a way to describe the format of text strings. You can write a pattern and then see if a string matches it, or look for all matches, or replace them all with something else, and so on. Here is an example of a regular expression matching typical (but not all) HTTP URLs with optional query strings and fragments:

http://([a-z0-9-]+\.)+[a-z0-9-]+/[^\s#?]*(\?[^\s?#)+?(#[^\s?#)+?

Regex Implementations

J2SE 1.4 supports regular expressions natively in the java.util.regex package. For earlier versions of Java, or for more choices, you can use:

A comparison of the above libraries is available here http://www.oreilly.com/catalog/regex2/chapter/ch08.pdf

JUnit tests for regexes

  • Use regex asserts for testing your application
    • with the sun java 1.4 regex implementation
    • with Jakarta Regexp
    • with the regex implementation you want
  • Run test files for the regex implementation
    • Run your own testfiles
    • Run the Jakarta Regexp tests "docs/RETest.txt"
    • Run other implementation tests

Please leave a note on the H&P Open Source Wiki

Also See

Mastering Regular Expressions by Jeffrey Friedl at http://www.oreilly.com/catalog/regex2/ is a good introduction and covers advanced usage, including Java (this chapter is available online).

Java Regular Expressions: Taming the java.util.regex Engine by Mehran Habibi at http://www.apress.com/book/bookDisplay.html?bID=177 for complete coverage of the java regex package.



Discussion about RegularExpressions

Topic RegularExpressions . { Edit | Ref-By | Printable | Diffs r11 < r10 < r9 < r8 < r7 | More }
 XML java.net RSS

Revision r11 - 31 Jul 2007 - 12:06:26 - Main.scolebourne