Many languages, including Perl, PHP, Python, JavaScript, and JScript, support the use of regular expressions to process text, and some text editors implement advanced search-replace functionality with regular expressions. So the Java language is no exception. Regular expressions have gone beyond the limits of a language or a system and become a widely used tool, and we can use it to solve some of the practical problems encountered in actual development.
Basic knowledge of regular expressions
1.1 Period Symbol
Suppose you are playing English Scrabble and want to find three-letter words that must begin with the letter "T" and End With "n" Letters. In addition, suppose you have an English dictionary, you can use regular expressions to search all of its contents. To construct this regular expression, you can use a wildcard character-the period symbol ".". In this way, the complete expression is "T.N", which matches "tan", "ten", "Tin" and "ton", and also matches "T#n", "TPN" or even "T n", and many other meaningless combinations. This is because the period symbol matches all characters, including spaces, tab characters, and even line breaks:
1.2 square brackets Symbol
In order to solve the problem that the period symbol matching range is too broad, you can specify a meaningful character in square brackets ("[]"). At this point, only the character character character specified in the square brackets participate in the match. That is, the regular expression "t[aeio]n" matches only "tan", "Ten", "Tin", and "ton". But "Toon" does not match, because within the square brackets you can only match a single character:
1.3 "or" symbol
If you want to match "toon" in addition to all the words above, you can use the "|" Operator. | The basic meaning of an operator is the "or" operation. to match "Toon", use the "t (A|e|i|o|oo) n" Regular expression. You cannot use a square extension here because the brackets allow only a single character to be matched, and you must use the parentheses "()" here. Parentheses can also be used to group.
1.4 Symbols that indicate the number of matches
The following table shows the syntax for regular expressions:
Table 1.1 Regular Expression syntax
Suppose we want to search the U.S. Social Security number in a text file. The format of this number is 999-99-9999. The regular expression used to match it is shown in figure one. In a regular expression, a hyphen ("-") has a special meaning, which represents a range, for example, from 0 to 9. Therefore, when matching the hyphenation symbol in the social security number, it is preceded by an escape character "/".
Suppose you want the hyphen to appear or not when you are searching-that is, 999-99-9999 and 999999999 are in the correct format. At this point, you can add the word "? The quantity qualifier symbol.
A format for U.S. car licences is four digits plus two letters. Its regular expression is preceded by the number part "[0-9]{4}", plus the letter part "[A-z]{2}".
1.5 "no" symbol
The "^" symbol is called a "no" symbol. If used in square brackets, "^" denotes a character that you do not want to match. For example, the regular expression in Figure four matches all words except words that begin with the "X" letter.
1.6 parentheses and blank symbols
The "/S" symbol is a blank symbol that matches all whitespace characters, including the tab character. If the string matches correctly, then how do you extract the month portion? Simply create a group with parentheses around the month, and then extract its value with the Oro API.
1.7 Other symbols
for simplicity, you can use some shortcut symbols that are created for common regular expressions. As shown in the following:
/t: tab, equivalent to/u0009
/N: line breaks, equivalent to/u000a
/d: Represents a number, equivalent to [0-9]
/d: Represents a non-numeric, equivalent to [^0-9]
/s: white space characters that represent line breaks, tab tabs, and so on
/S: Represents Non-white-space characters
/w: Alphabetic characters, equivalent to [a-za-z_0-9]
/w: Non-alphabetic characters, equivalent to [^/w]
For example, in the previous example of social Security numbers, we can use "/d" for all occurrences of "[0-9]".
Two, the following is I sorted out the procedure for reference:
Package org.luosijin.test;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Regex {public static void main (string[] args) {Pattern pattern = pattern.compile ("B*g");
Matcher Matcher = Pattern.matcher ("BBG");
System.out.println (Matcher.matches ());
System.out.println (Pattern.matches ("B*g", "BBG"));
Verify the ZIP code System.out.println (pattern.matches ("[0-9]{6}", "200038"));
System.out.println (Pattern.matches ("//d{6}", "200038")); Verify the phone number System.out.println (Pattern.matches ("[0-9]{3,4}//-?[
0-9]+ "," 02178989799 "));
GetDate ("Nov 10,2009");
Charreplace ();
Verify ID: Determine if a string is an ID number, that is, 15 or 18 digits.
System.out.println (Pattern.matches ("^//d{15}|//d{18}$", "123456789009876"));
GetString ("D:/dir1/test.txt");
Getchinese ("Welcome to", Jiangxi Fengxin, welcome, you!);
Validateemail ("luosijin123@163.com"); /** * Date Extraction: Extract Month to * * public static void GetDate (String str) {string regex= ([a-za-z]+) |//s+[0-9]{1,2},//s*[
0-9]{4} ";Pattern pattern = pattern.compile (regEx);
Matcher Matcher = Pattern.matcher (str);
if (!matcher.find ()) {System.out.println ("Wrong date format!");
Return } System.out.println (Matcher.group (1));
The index value of the grouping starts at 1, so the first grouping is M.group (1) instead of M.group (0).
/** * Character substitution: This example replaces all occurrences of one or more contiguous "a" in a string with "a".
*/public static void Charreplace () {String regex = "A +";
Pattern pattern = pattern.compile (regex);
Matcher Matcher = Pattern.matcher ("okaaaa letmeaseeaaa aa Booa");
String s = Matcher.replaceall ("A");
System.out.println (s);
/** * String fetch/public static void GetString (String str) {string regex = ". +/(. +) $";
Pattern pattern = pattern.compile (regex);
Matcher Matcher = Pattern.matcher (str); if (!matcher.find ()) {System.out.println ("file path is malformed!")
");
Return
} System.out.println (Matcher.group (1)); /** * Chinese Extract * @param str * @author Rosikin * @date 2009-11-10 a.m. 12:27:17/public static void Getchinese ( String Str{String regex = ' [//u4e00-//u9fff]+ ';//[//U4E00-//U9FFF] is the Chinese character pattern = pattern.compile (regex);
Matcher Matcher = Pattern.matcher (str);
StringBuffer sb = new StringBuffer ();
while (Matcher.find ()) {Sb.append (Matcher.group ());
} System.out.println (SB); public static void Validateemail (string email) {String regex = ' [0-9a-za-z]+@[0-9a-za-z]+//.[
0-9a-za-z]+ ";
Pattern pattern = pattern.compile (regex);
Matcher Matcher = pattern.matcher (email);
if (Matcher.matches ()) {System.out.println ("This is a legitimate email");
}else{System.out.println ("This is an illegal email"); }
}
}
above is the full content of Java regular expressions, and I hope to help you with your learning.