Regular Expression:
Regular expressions have two standards: Basic Regular Expressions (BRE) and extended regular expressions (ERE ). Ere includes bre functions and other concepts.
Application
Program Text processing functions such as Word search, email confirmation, and XML document integration are often required. This usually involves pattern matching. Perl, sed, awk, and other languages use regular expressions to improve pattern matching. A regular expression is a string of characters, and the pattern defined by it can be used to find matched texts. To use javatm
Programming Language For pattern matching, you need to use the stringtokenizer class with many charat sub-strings to read letters or symbols for text processing. This often leads to complicated or messy
Code .
Now it's different:
The Java 2 platform Standard Edition (j2se) 1.4 contains a new software package named java. util. RegEx, which makes it possible to use regular expressions. The current features include the use of metacharacters, which gives the regular expression great flexibility.
//--------------------------------------------------------------------------
Common applications of Regular Expressions:
1: simple word replacement
2: email confirmation
3: delete control characters from the file
4. Search for files
Regular Expression Java class:
1: pattern class
2: matcher class
What is a mode?
In the context of a regular expression, the pattern is the text representation of the Character Sequence. For example, if you want to know whether the word car exists in a character sequence, you will use the mode car because this is a method that accurately represents the string. For more complex modes, you can use special characters as placeholders. If you do not want to search for a car, but want to search for any text string that starts with the letter C and ends with the letter R, you will use the C * r mode, * represents any number of characters before the first R. The C * r mode matches any string that starts with C and ends with R, such as Cougar, Cavalier, or Chrysler.
How to specify a pattern expression
The main part of pattern matching is about the expression to be used. Pattern first saves the expression to be used, and then passes it to the matcher class to check its matching condition in the context of the Character Sequence. For example, if you want to verify an email address, you may want to check whether the user input matches this pattern-it contains a sequence of letters and numbers followed by a @ symbol, @ and then two groups of characters separated by periods. This can be expressed by the expression \ P {alnum} + @ \ W + \. \ P {Alpha} {2, 3. (This obviously simplifies the email address structure and may exclude some valid email addresses, but it is sufficient as an example .)
Before discussing the details of the mode language, let's take a closer look at \ P {alnum} + @ \ W + \. \ P {Alpha} {2, 3 }. The \ P {alnum} sequence represents a single alphanumeric character (A to Z, A to Z, or 0 to 9 ). The plus sign (+) after \ P {alnum} is called a quantizer ). It is applied to the first part of the expression, indicating that \ P {alnum} must appear once or more times. Use the asterisk (*) to indicate zero or more times (including once ). @ Means that it must appear after at least one letter or number character, so that the entire pattern match can be successful. \ W + is similar to \ P {alnum} +, but underlines (_) are added (_). Some sequences have multiple expressions. The backslash (\.) indicates the period. If there is no backslash before, a separate period represents any character. The last \ P {Alpha} {2, 3} represents two or three letter characters.
//-------------------------------------------------------------------------
Once you have learned the standard language, you can master all the secrets of the pattern. Let's look at some of the more common expressions:
Literal: any character in an expression that has no special meaning is considered as a text and matches itself.
Quantifiers: certain characters or expressions used to calculate the number of times a text or group can appear in a character sequence so that the sequence matches the expression. A group is specified by a group of characters in parentheses.
? Indicates that one or no
* Indicates zero occurrence or more than once (inclusive)
+ Indicates one or more occurrences
Character class: A character class is a character set in square brackets. Matching can be any character in square brackets. You can combine character classes with quantifiers. For example, [acegikmoqsuwy] * is a sequence of any character that only contains odd letters in the alphabet. Some character classes are pre-defined:
\ D-number (0 to 9)
\ D -- Non-numeric
\ S -- blank characters, such as tabs or line breaks
\ S -- Non-blank characters
\ W -- single character (A to Z, A to Z, 0 to 9, and underline)
\ W -- non-single character (Other arbitrary characters)
POSIX character class (POSIX character class): some character classes are valid only when used for US-ASCII comparison. For example:
\ P {lower}-lowercase characters
\ P {upper}-uppercase characters
\ P {ASCII}-All ASCII characters
\ P {Alpha}-letter (\ P {lower} And \ P {upper)
\ P {digit}-number from 0 to 9
\ P {alnum}-alphanumeric characters
Range: Dash is used to specify character classes that contain a certain range of characters. For example, [A-J] represents an uppercase letter from A to J.
Negation: removes the character (^) to deny the content of the character class. For example, [^ A-J] represents any character except A to J.
//------------------------------------------------------------------------
How to Use the mode effectively?
Now that you know how to specify the modes, let's use them. You need to let the pattern class compile them, as shown below. Note that the backslash character (\) must be escaped in the String constant.
Pattern pattern = pattern. Compile (
"\ P {alnum} + \ W + \. \ P {Alpha} {2, 3 }");
With a compiled mode, you can use the pattern class to split an input line into a series of single words based on the mode, or use the matcher class to execute more complex tasks. The following describes how to split the input character sequence. The mode used specifies the separator instead of the word:
String words [] = pattern. Split (input );
If you want to match a pattern multiple times in a character sequence, the above code snippet is a good starting point. However, if you want to obtain a specific input, you need the matcher () method of pattern. Given an input, this method returns an appropriate matcher class. Next, you can use the matcher instance to traverse the entire result and search for different pattern matches in the input sequence, or use the matcher instance as the search-replace tool (the latter method is better ):
Matcher = pattern. matcher (input );
Use matches () to match the entire sequence (). To determine whether only part of the sequence matches each other, use find ():
If (matcher. Find ()){
// Found some string within input sequence
// That matched the compiled Pattern
String Match = matcher. Group ();
// Process matching pattern
}
These two classes (pattern and matcher) are the entire pattern matching library. Put forward the correct Regular Expression and then use the matcher class result. This is all the work done by the pattern matching library.
In Java programming, you can use regular expressions wherever you need pattern matching.