This article describes how to use regular expressions in Java to work with text data. A regular expression is a string, but unlike a regular string, a regular expression is an abstraction of a set of similar strings, such as the following strings:
a98b c0912d c10b a12345678d AB
We analyze the five strings above to see that they have a common feature that the first character must be ' a ' or ' C ', the last character must be ' B ' or ' d ', and that the middle character is any number of digits (including 0 digits). So we can abstract the common features of these five strings, which produces a regular expression: [AC]\\D*[BD]. And based on this regular expression, we can write infinite number of strings that satisfy the condition.
There are many ways to use regular expressions in Java, and the simplest is to use them with strings. There are four methods in string that can use regular expressions, they are matches, split, ReplaceAll, and Replacefirst.
First, Matches method
The matches method can determine whether the current string matches a given regular expression. Returns true if a match, otherwise, returns false. The matches method is defined as follows:
Copy Code code as follows:
Public boolean matches (String regex)
As the regular expression given above we can use the following program to verify.
<!---->string[] ss = new string[]{"a98b", "c0912d", "c10b", "a12345678d", "AB"};
for (String s:ss)
System.out.println (S.matches ("[AC]\\D*[BD]"));
Output results:
True
True
True
True
True
Here's a quick explanation of what this regular expression means. If we have learned the lexical analysis of the compiler principle, it is easy to understand the regular expression above (because the expression of the regular expression is similar to that in the lexical analysis). As in [...] The equivalent or "|" in, such as [ABCD] is equivalent to a|b|c|d, i.e. A or B or C or D. As the beginning of the regular expression above is [AC], it means that the beginning of the string can only be a or C. The [BD] expression string ending can only be B or D. And the middle of the \d expression 0-9 of the number, because \ in the regular expression has a special meaning, so that \ \. and * indicates that there are 0 or infinitely many (which are called * closures in lexical analysis), and because of the * following the \d, there are 0 or infinitely many digits expressed.
Second, split method
The split method uses a regular expression to split the string and returns the result as a string array. Split has two overloaded forms, which are defined as follows:
<!---->public string[] Split (string regex) public
string[] Split (string regex, int limit)
The following code will use the first overloaded form of split to split the first line of the HTTP request header, as follows:
<!---->string s = "get/index.html http/1.1";
String ss[] = s.split ("+");
for (String str:ss)
System.out.println (str);
Output results:
Get
/index.html
http/1.1
When using the first overloaded form of split, it should be noted that if the split string finally has an empty string, it is ignored. If you use regular expression \d to split the string a0b1c3456, the resulting array has a length of 3 instead of 7.
There is a limit parameter in the second overloaded form of split, which is discussed in three different situations:
1. Greater than 0: if the value of limit is n, then the regular expression will be used n-1 times, the following code:
<!---->string s = "a0b1c3456";
String ss[] = S.split ("\\d", 3);
for (String str:ss)
System.out.println (str);
Output results:
A
B
c3456
As you can see from the output, the program only uses two regular expressions for "a0b1c3456", that is, after the character ' 1 ' is scanned less, the string that follows is used as a whole to return the last value of the array, regardless of whether there is a string that satisfies the condition.
2. Less than 0: do not ignore the end of the empty string. The above example returns the length of the array to be 7 instead of 3.
3. Equals 0: This is the default value, equivalent to the first overloaded form of split.
Iii. ReplaceAll and Replacefirst methods
The definitions for the two methods are as follows:
public string ReplaceAll (string regex, string replacement) public
string Replacefirst (string regex, String Replacement)
The two methods replace the replacement string with the regex in the current string. The use of the method is simple, no longer detailed here, interested readers can refer to the relevant documentation.