Overview
Have you ever used to find *.tmp temporary files?
Have you ever used the Find/Replace feature in the editing tool?
In fact, they all use regular expressions
The role of regular expressions
Provides more powerful string processing capabilities
To test patterns within a string
For example, you can test the input string to see if the phone number pattern or the ID number pattern appears within the string. i.e. data validation
Replace text
You can use regular expressions to identify specific text in a document, to completely delete the text, or to replace it with other text.
Extract substrings from strings based on pattern matching
You can find specific text within a document or in an input field.
Using regular Expressions in Java
The regular expression has a powerful function in string processing, and sun has added support for it in jdk1.4
The Java.util.regex package is included in jdk1.4 to provide support for regular expressions. And the ReplaceAll and split functions in the Java.lang.String class are also implemented by regular expressions called.
The action of a regular expression on a string consists mainly of:
1. String Matching
2. Specify string substitution
3. Specify string Lookup
4. String segmentation
Pattern class
An instance of the pattern class represents a regular expression specified as a string, with a syntax similar to that used in Perl.
A regular expression that is specified as a string must first be compiled into an instance of the pattern class. The generated pattern is used to create the Matcher object, which matches any sequence of characters based on the regular expression. Multiple matches can share a pattern because it is not exclusive
The given regular expression is compiled into a pattern using the compile method, and then a match is created using the Matcher method, which matches the given input according to this pattern. The pattern method returns the regular expression used to compile the schema.