A little bit more...

Friday, July 27, 2007

Regular expression and CharSequence in Java

Introduction
Regular expression, specified as a string, is first compiled into an instance of Pattern. The regular expression constructs supported by Pattern are almost the same (see comparison to Perl 5 in javadoc of class pattern) as that supported by Perl 5. The Pattern engine performs traditional NFA-based (Nondeterministic Finite Automate) matching with ordered alternation as occurs in Perl 5.

The regex Package
Two major class Pattern and Matcher, both of which have non-public constructor which means  the client can't new instances of them directly. A Pattern instance is the compiled intermediate representation, plus some helper methods, of the string form regular expression. Instances of Pattern are immutable which means they're thread-safe. And each instance of Matcher, produced by factory method of Pattern, corresponds to each matching between the bound regular expression of the target string ( i.e., the string to be matched). Further information about how to use this package in a program can be found in the resources section.

regex matching in CharSequence
Despite being as part of the java library, the regex package is also used by other parts of the library. These usages are listed below:
boolean String.matches( String regex );
String String.replaceAll( String regex, String replacement )
String String.replaceFirst( String regex, String replacement )
String[] split( String regex )

Note that rather than class String or some other concrete representation of CharSequence, interface CharSequence is widely depended upon both in Pattern and String. Classes implementing CharSequence mainly include String, StringBuffer and StringBuilder. And the implemented methods include such methods that we usually use with String as charAt(int), length(), and subSequence(int, int).


Resources
  1. Interface java.lang.CharSequence
  2. Package java.util.regex
  3. Lesson: Regular Expressions

No comments:

About Me

My photo
I'm finishing my master degree in Software Engineering, Computer Science. I believe and have been following what Forrest Gump's Mam said: you have to do the best with what god gave you.