Regular expression
-
Regular expression uses a single string to describe it, which matches a series of search mode that conforms to a string that conforms to the rules of a sentence
-
Use some mode to match the formula of a type of string
-
Regular expression is similar to the contract in life. Use a special symbol to operate the string to determine whether the strings meet the rules of matching.
-
Regular role:
- Test the mode in the string
- replace text
- Extract the sub -string from the string
-
advantage: great simplify code
-
Disadvantages: Poor reading
character | matching | usage |
---|---|---|
. | All characters | Note:\\. Represents. |
\ | turning symbol | java to use the following characters to add \ |
\d | All numbers | \\d |
\D | non -numeric[^0-9] |
\\D |
[0-9] | All digital effects are the same as \ d | |
[a-z] | All lowercase letters | |
[A-Z] | All uppercase letters | |
\s | A space | \\S |
\w | All letters, numbers, stroke lines, equivalent to[a-zA-Z_0-9] |
\\W |
\w | Non -word character[^\w] |
\\W |
^ | indicates the beginning | Note:^[0-9] and
The difference between |
$ | indicate ending | |
[\u4e00-\u9fa5] |
matching Chinese characters | There is a matter of 100, there are alcohol punishment me |
? | indicates that the number of characters appear 0 or once | |
+ | once or multiple times | |
* | zero or multiple times | |
{n} | N times | |
{n,} | At least N times | |
{n,m} | at least N times, at most M times |
// Verify mobile phone number: 1. length 11 digits 2. Pure numbers 3. The second digit is [35789]
private static void fun1() {
String phone="13565248952";
String reg="[1][35789][0-9]{9}";
String reg1="1[35789]\\d{9}";
boolean f = phone.matches(reg1);
System.out.println(f);
}
- Term Explanation: Cut the specified string according to the given rules (regular expression).
// Cut
private static void fun2() {
String names="Zhang San. Li 4. Wang Ermazi";
String[] ns = names.split("\\.");
System.out.println(Arrays.toString(ns));
}
- Term Explanation: Replace the specified string according to the given rules (regular expression) to a new string, the return value is string
// Replace-telephone
private static void fun3() {
String info="My doll has better experience, contact information: 13538384938";
String newInfo = info.replaceAll("1[35789][0-9]{9}", "110");
System.out.println(newInfo);
}
- Filter stack words
// Replace-stacking words-group
private static void fun4() {
String s="You are so fat, there are many meaty meat, you are good or bad!";
// string reg = "(.) \\ 1 {1,}"; // 1: another time, twice
String reg="(.)\\1+";// 1: Once again, twice in total
String ss = s.replaceAll(reg, "$1");
System.out.println(ss);
}
- (.) Means matching any character, as a group
- (. (A)) The overall group is the first group
\\1
, inner layer (A) as the second group\\2
- $ 1 is used to remove the first group, $ 2 for the second group
// Replace-stacking words-group
private static void fun5() {
// Differential matching and obtaining
String s="Eat";
String reg="(. (Rice) {2})" "";
// string reg = "(. (Rice)) \\ 2";
String ss = s.replaceAll(reg, "$1");
System.out.println(ss);// Eat for meals
String s1="Eat and meals";
String reg2="(. (Rice) {2}) \\ 1";
String h = s1.replaceAll(reg2, "$1");
System.out.println(h);// Eat meals
}
// 1: Bai Bing: I like you
private static void fun6() {
String s="Bai Bing: I like to happy ... happy ... happy .. Huan ... Huanhuan .. Huanhuan. Huan .... You";
// 1. Replace. ""
String reg="\\.+";
String s1 = s.replaceAll(reg, "");
System.out.println(s1);
// 2. Remove words-grouping I like to love you
String reg2="(.)\\1+";
String s2 = s1.replaceAll(reg2, "$1");
System.out.println(s2);
}
- mailbox verification
/*
Mail
Module 1: Numbers, letters, lower line length 3-12
Module 2: Either pure numbers or pure letters, length 2-6
Module three: .com.cn appeared 1-3 times
*/
private static void fun7() {
String mail="[email protected]";
String reg="\\w{3,12}@(\\d{2,6}|[a-zA-Z]{2,6})(\\.[a-z]{2,3}){1,2}";
boolean f = mail.matches(reg);
System.out.println(f);
}
- Age
// Age 130 1-99 | 100-129 | 130
private static void fun8() {
String age="-13";
String reg="[1-9]\\d?|1[012]\\d|130";
System.out.println(age.matches(reg));
}
- Chinese characters and spaces
private static void fun9() {
String s="Hello";
String reg="^[\\u4e00-\\u9fa5]{2}\\s{2}";
boolean m = s.matches(reg);
System.out.println(m);
}
<form id="form1" action="http://111.13.100.91/" method="post">
<table border="1" width="600">
<tr>
<td>Nickname:</td>
<td><input type="text" name="nick" pattern=".{4,10}" required placeholder="Please enter 4-10 characters"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pwd" pattern=".{6,16}" required placeholder="Please enter 6-16-bit characters"></td>
</tr>
<tr>
<td>mobile number:</td>
<td><input type="text" name="phone" pattern="1[35789]\d{9}" required placeholder="Please enter the correct mobile phone number"></td>
</tr>
<tr>
<td>mailbox:</td>
<td><input type="email" name="email" required placeholder="Please enter the correct mailbox format"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" pattern="^[1-9]\d*$" name="age" placeholder="Please enter a positive number"></td>
</tr>
<tr>
<td><input type="submit" value="Registration"></td>
<td><input type="reset" value="reset"></td>
</tr>
</table>
</form>