Friday, January 28, 2011

regexp's in Java...

http://www.programmersheaven.com/2/RegexJAVA
Regular Expressions In JAVA
The JAVA class library provides two classes to support regexes in JAVA, namely Pattern and Matcher from the java.util.regex package. Pattern simply stores a regex that we want to match strings against. We can get a Matcher object by matching a Pattern against a String, and then use the Matcher object to see if the pattern matched, extract captures, etc.
An Annoying JAVA Issue
Many regexes use sequences such as \d to match a digit. As in many programming languags, a regex in JAVA is represented as a string. This is a problem because "\d" is seen by the JAVA compiler as a JAVA escape sequence, and the compiler will complain that it doesn't understand the escape sequence \d. Therefore, it is important to add extra backslashes, e.g. "\\d". Be careful - in the case of \b this will go quietly un-noticed at compile time, then your regex won't work out as you expect at runtime.

No comments:

Post a Comment