|
Java™ by example!
|
|
|
How do I get started with the 1.4 package regex?
If you have no clue about what regular expressions are, check out Introduction to Regular Expressions by Tom Sgouros. First, define a regular expression:
Then you can apply that pattern to a sequence of characters (the interface CharSequence, which String, StringBuffer and CharBuffer implement, has been added to JDK1.4. You can then create a Matcher providing it with a CharSequence to which you want to apply your pattern:
Executing matches() returns true when the whole region matches the pattern. find() returns true when there is a subsequence of the region that matches the pattern. You can find the start and end indexes with start() and end(). For example: MatchString.java:
outputs:
To test out different regular expressions (find a summary of regular-expression constructs in the Javadoc documentation of Pattern), you can use the following example. It prints out a series of matches, given an input file and a regular expression. Main.java:
Example:
outputs:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|