Applications often require text-processing functions, such as Word lookup, e-mail acknowledgement, or XML document integration. This usually involves pattern matching. Languages such as Perl, sed, or awk use regular expressions to improve pattern matching, and the regular expression is a string of characters that can be defined to find matching text. In order to use the JavaTM programming language for pattern matching, you need to use the StringTokenizer class with many charat substrings to read letters or symbols to work with text. This often leads to complex or messy code.
It's not the same now.
The 2 Platform Standard Edition (J2SETM) version 1.4 contains a new package called Java.util.regex that makes it possible to use regular expressions. Current features include the use of meta characters, which give the regular expression great flexibility
This article provides an overview of the use of regular expressions and explains in detail how to use regular expressions using the Java.util.regex package, using the following common scenarios as examples:
Simple word substitution
Email Confirmation
Remove control characters from a file
Find Files
To compile the code in these examples and use regular expressions in your application, you need to install J2SE version 1.4.
Constructing regular Expressions
A regular expression is a character pattern that describes a set of strings. You can use the Java.util.regex software package to find, display, or modify part or all of a pattern that appears in the input sequence.
The simplest form of a regular expression is an exact string, such as "Java" or "programming". Regular expression matching also allows you to check whether a string conforms to a specific syntactic form, such as an e-mail address.
In order to write regular expressions, both normal and special characters are used:
Any other character that appears in a regular expression is a normal character unless it has a \ in front of it.
Special characters are of particular use. For example,. You can match any character except a line break. A regular expression such as S.N matches any three-character string that begins with N, including Sun and son.
There are many special characters in a regular expression that can look up a line at the beginning of a word, ignore case or case sensitive words, and special characters can give a range, such as A-E represents any letter from a to E.