11 Regular Expressions
An expression that conforms to a certain rule.
2. Use
Used to specialize in manipulating strings.
The matches (string regex) method is provided in string to inform whether this string matches a given regular expression.
2. Features
Use some specific symbols to represent some code manipulation, which simplifies writing.
3. Benefits
You can simplify complex operations on strings.
4. Disadvantages
The more symbol definitions, the longer the regular expression, the worse the reading.
5, the QQ number to verify, 5-15 digits, can only be the number.
public class QQ {
public static void Main (string[] args) {
String qq= "81276490";
String regex = "[1-9][0-9]{4,14}";
Boolean B = qq.matches (regex);
if (b) {
SYSTEM.OUT.PRINTLN ("correct");
}else {
SYSTEM.OUT.PRINTLN ("error");
}
}
}
which
[1-9] indicates that only a number between 1 and 9 can be taken.
[0-9] indicates that only a number between 0 and 9 can be taken.
{4,14} represents a minimum of 4 repetitions and repeats up to 14 times.
11.1 Regular Expression rules (only some of the common ones are listed)
1. Character class |
[ABC] |
a,b or C(Simple Class) |
[^ABC] |
Any character except a,b , or C(negation) |
[A-za-z] |
a to z or a to z, the letters at both ends are included (range) |
For example: String s = "a";
String regex = "[A,b,c]";
Boolean B = s.matches (regex);
System.out.println (b);
2, predefined character class |
. |
any character (may or may not match the line terminator) |
\d |
number: [0-9] |
\d |
non-numeric: [^0-9] |
\s |
whitespace characters: [\t\n\x0b\f\r] |
\s |
non-whitespace characters: [^\s] |
\w |
Word character: [a-za-z_0-9] |
\w |
non-word characters: [^\w] |
Example: String s = "A9";
String regex = "[a-za-z]\\d";
Boolean B = s.matches (regex);
System.out.println (b);
3. Boundary Matching Device |
^ |
The beginning of the line |
$ |
End of Line |
\b |
Word boundaries |
\b |
Non-word boundary |
4, greedy number of words |
X ? |
X, not once or once |
X * |
X, 0 or more times |
X + |
X, one or more times |
X {n} |
X, exactly n times |
X {n,} |
X, at least n times |
X {n,m} |
X, at least n times, but not more than m Times |
Example: The number after the letter appears 0 or more times.
String s = "A98";
String regex = "[a-za-z]\\d*";
Boolean B = s.matches (regex);
System.out.println (b);
5. Logical operator |
Xy |
X followed by Y |
X | Y |
X or Y |
(X) |
X, as capturing group |
6, matching mobile phone number: 13...,15...,18 ...
public static void Checktel () {
String s = "13812930477";
String regex = "1[358]\\d{9}";
Boolean B = s.matches (regex);
System.out.println (b);
}
11.2 Regular Expression cut-string
1, cut the string with a space, the number of occurrences of the space is not determined.
public class Splitdemo {
public static void Main (string[] args) {
& nbsp; splitdemo ();
}
public static void Splitdemo () {
&NBSP
string s = "zhangsan lisi Wangwu" ;
string regex = "+";
string[] arr = s.split (regex);
system.out.println (arr.length);
for (String string:arr) {
system.out.println (string);
}
}
}
2, using. To cut strings
public static void Splitdemo () {
String s = "Zhangsan.lisi.wangwu";
String regex = ".";
string[] arr = s.split (regex);
System.out.println (arr.length);
for (String String:arr) {
System.out.println (string);
}
}
The output result array length is 0.
Because "." is a special symbol in regular expressions, so you need to escape "\ \" when needed.
3. Use overlapping words to cut strings
public static void Splitdemo () {
String s = "Aczzukksiyyyhiiiiiok";
String regex = "(.) \\1+ ";
string[] arr = s.split (regex);
System.out.println (arr.length);
for (String String:arr) {
System.out.println (string);
}
}
In order for the results of the rule to be reused, the rules can be encapsulated into a group and completed with (). Groups are numbered, starting with 1. You want to use an existing group to get it in the form of a \ number.
11.3 Regular Expression substitution string
A string method is used replaceFirst(String regex, String replacement)
.
1. Replace the number in the string with 4 or more times *. such as dada23325435f123dsf88943der3442
public class Replacealldemo {
public static void Main (string[] args) {
String s = "dada23325435f123dsf88943der3442";
String regex = "\\d{4,}";
Replacealldemo (S, Regex, "*");
}
public static void Replacealldemo (String str,string regex,string newstr) {
str = Str.replaceall (regex, NEWSTR);
System.out.println (str);
}
}
Results: dada*f123dsf*der*
2. Replace the overlapping words in the string with #. such as Aczzukksiyyyhiiiiiok
public class Replacealldemo {
public static void Main (string[] args) {
String s = "Aczzukksiyyyhiiiiiok";
String regex = "(.) \\1+ ";
Replacealldemo (S, Regex, "#");
}
public static void Replacealldemo (String str,string regex,string newstr) {
str = Str.replaceall (regex, NEWSTR);
System.out.println (str);
}
}
Results: Ac#u#si#h#ok
3. Replace the overlapping words in the string with one.
public class Replacealldemo {
public static void Main (string[] args) {
String s = "Aczzukksiyyyhiiiiiok";
String regex = "(.) \\1+ ";
Replacealldemo (S, Regex, "$");
}
public static void Replacealldemo (String str,string regex,string newstr) {
str = Str.replaceall (regex, NEWSTR);
System.out.println (str);
}
}
Results: Aczuksiyhiok
11.4 Regular expressions to get sub-strings
1. Operation procedure
Encapsulates a regular expression into an object pattern
To correlate a regular expression object with a string to manipulate
After association, get the regular match engine
Working with rules-compliant substrings through the engine
2. Intercept strings of 3 consecutive characters from a string
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Substringdemo {
public static void Main (string[] args) {
Substringdemo ();
}
public static void Substringdemo () {
String s = "Hei ma wo Lai le,yi ying Yao Hao hao Xue Xi";
String regex = "\\b[a-z]{3}\\b";
Encapsulating a regular expression as an object
Pattern p = pattern.compile (regex);
The regular is associated with the string to be manipulated
Matcher m = P.matcher (s);
Apply a rule to a string and make a rule-compliant substring lookup
while (M.find ()) {
System.out.println (M.group ());
}
}
}
11.5 web crawler
1. Get the email address in the specified Web page
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.URL;
Import java.net.URLConnection;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Substringdemo {
public static void Main (string[] args) throws IOException {
Getmails ();
}
public static void Getmails () throws ioexception{
URL url = new URL ("http://192.168.1.11:8080/myweb/text.html");
URLConnection conn = Url.openconnection ();
BufferedReader br = new BufferedReader (New InputStreamReader (Conn.getinputstream ()));
String line = null;
String Mailreg = "\\[email protected]\\w+ (\\.\\w+) +";
Encapsulating a regular expression as an object
Pattern p = pattern.compile (Mailreg);
while (null!= (Line=br.readline ())) {
The regular is associated with the string to be manipulated
Matcher m = p.matcher (line);
Apply a rule to a string and make a rule-compliant substring lookup
while (M.find ()) {
System.out.println (M.group ());
}
}
Br.close ();
}
}
This article is from the "Java Basics" blog, so be sure to keep this source http://8163413.blog.51cto.com/8153413/1713956
11th-Regular Expressions