regular expression
-
Regular expression: defines the matching pattern of the string
-
It can be used to search, edit or process text. It is not limited to one language, but there are subtle differences in each language
-
JDK1.4. Java util. Regex package, which well supports regular expressions
-
Common syntax:
-
\A transfer character that converts a meaningful character into a meaningless character
-
But \ in java is also a transition character, so when writing regular expressions in java, you need two transition characters\
-
Related to character value range
-
[abc]: indicates that it may be a, b, or c (any one of abc)
-
[^ abc]: indicates that it is not any of a, B and C
-
[a-zA-Z]: indicates upper and lower case letters, [A-Z] lowercase letters, [A-Z] uppercase letters [0-9] numbers 0-9
-
[a-zA-Z0-9]: indicates numbers or letters
-
Concise representation
-
.: matches any character
-
\d: indicates that the number is equivalent to [0-9]
-
\D: non numeric equivalent to [^ 0-9]
-
\s: indicates that it is composed of empty characters, [\ t\n\r\x\f]
-
\S: indicates the composition of non empty characters
-
\w: indicates that it is composed of alphanumeric underscores [a-zA-Z0-9_]
-
\W: non alphanumeric underscore [^ a-zA-Z0-9]
-
Quantity
-
? : Indicates 0 or 1 occurrences
-
+: Indicates one or more times, Greater than or equal to 1
-
* : Any number of times(0~N)
-
{n} : indicates n occurrences, {2} indicates 2 occurrences
-
{n,m}: n to m times, {2,5}: 2 to 5 times
-
{n,}: indicates N and above > = n
-
(): see them as a whole
-
|Or, a|b, a and B can match
-
^: start with what, but when used in [^ xxxx], it means negative
-
$: end with what
-
a[a-z] , a1 , aA , a3, 22
-
Check non Chinese characters
-
[^\u4e00-\u9fa5]
-
Pattern: to create regular expression objects, you can do some basic and simple operations
-
Three functions:
-
Verification: boolean matchers(String regex);
-
Split: String[] split(String regex);
-
Replacement: String replaceAll(String regex,String replacement);
-
In practice, sometimes we find it troublesome to directly use some methods in String, such as replacement, splitting and verification. There are also methods in String classes
public static void main(String[] args) { // test_01(); test_02(); } // Verification: boolean matchers(String regex); public static void test_02() { String regex = "(-)?\\d+(\\.\\d+)?"; String str = "2.43"; // Whole word matching boolean flag = Pattern.matches(regex, str); System.out.println(flag); // The usage in the String class is also full word matching System.out.println(str.matches(regex)); } // Split String[] split(String regex); public static void test_01(){ String str = "1,2@3!4"; // Create regular expression object Pattern pattern = Pattern.compile("[^0-9]"); String[] strArray = pattern.split(str); for (String string : strArray) { System.out.println(string); } // String String[] strArray1 = str.split("[^0-9]"); for (String string : strArray1) { System.out.println(string); } }
public static void main(String[] args) { String regex = "\\d{11}"; String tel = "a131131131112"; // engine Pattern pattern = Pattern.compile(regex); // Matcher Matcher matcher = pattern.matcher(tel); // Three matching modes // matches: whole word match xxx // find: anywhere* xxx.* // lookingAt: from front to back xxx* // Whole word matching, which means ^ and$ System.out.println(matcher.matches()); // Note: a matcher object can be used together by calling the same method. Mixing is not recommended matcher = pattern.matcher(tel); // As long as the first one is qualified System.out.println(matcher.lookingAt()); matcher = pattern.matcher(tel); // Any position meets the conditions System.out.println(matcher.find()); }
//Data extraction public static void main(String[] args) { // You can use () to group the matched data. The first () is the first group and the second () is the second group // You can also not group. If you do data extraction without grouping, you can only extract the whole matching string // After grouping, the data of the corresponding group can be extracted // ((. {2,3}) phone number is) group 1 // (. {2,3}) group II // (\ \ d{11}) group 3 String string = "Zhang San's telephone number is 13111 Zhang San Si's telephone number is 13112 Zhang San Wu's telephone number is 13113 Zhang Si's telephone number is 13114 Wang Wu's telephone number is 13115 Zhao Liu's telephone number is 13113113333 sun Xiaohei's telephone number is 13113113211 and Xiao Hong's telephone number is 13113113311"; String regex = "((.{2,3})The phone number is)(\\d{11})"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(string); // find matches any position and stops when it matches one // If you then call the find method, you will continue to match back where you just stopped while (matcher.find()) { // Therefore, combined with the find feature, it can be used with group to extract data // Group: 0 or no parameter. Get the whole matched data. 1 is the first group and 2 is the second group // A parenthesis is a group System.out.println(matcher.group(2) + " : " + matcher.group(3)); } }