Regular expression:
A regular expression is a powerful tool that can be used for pattern matching and substitution. A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters) that describe one or more strings to be matched when finding the body of the text. A regular expression is used as a template to match a character pattern with the string being searched for.
Regular expressions play a very important role in character data processing, we can use regular expressions to complete most of the data analysis processing, such as: to determine whether a string is a number, whether it is a valid email address, from a large number of text data to extract valuable data, and so on, if you do not use regular expressions, Then the implemented program can be very long and error prone. I have a deep understanding of this, in the face of a large number of reference books electronic file collation work, if you do not know how to apply the regular expression to deal with, then it will be a very painful thing, the contrary will be able to easily complete, get a multiplier effect.
Since the purpose of this article is to introduce how to use regular expressions in Java, please refer to the readers who have just contacted the regular expressions, because of their limited space.
Java support for regular expressions:
The regular expression libraries are not included in the JDK1.3 or prior JDK versions for Java programmers to use, we used to use regular expression libraries provided by third parties, which are open source in the Third-party libraries and paid for, and are now included in the beta version of JDK1.4 with regular expression libraries---java . Util.regex.
So now that we have a lot of Java-oriented regular expression libraries to choose from, I'll introduce you to the two more representative Jakarta-oro and Java.util.regex, first of all, of course I've been using Jakarta-oro:
Jakarta-oro Regular Expression Library
1. Brief introduction:
Jakarta-oro is one of the most comprehensive and optimized regular expression APIs, the Jakarta-oro library formerly known as Oromatcher, was written by Daniel F. Savarese, who later donated it to Jakarta Project, Readers can download the API package from the apache.org Web site.
Many of the regular expression libraries that are open to the source code are regular expression syntaxes that support Perl5 compatibility, and Jakarta-oro regular expression libraries are no exception, and he is completely compatible with Perl 5 regular expressions.
2. Objects and their methods:
★patterncompiler object:
When we use the Jakarta-oro API package, the first thing to do is to create an instance of the Perl5compiler class and assign it to the Patterncompiler interface object. Perl5compiler is an implementation of the Patterncompiler interface, allowing you to compile the regular expression into a pattern object to match.
PatternCompiler compiler=new Perl5Compiler();