This article is based on a 5-year hammer practice-the most basic Java video for beginners in history
Example: Require QQ number length is 5~15 bit, cannot start with 0
String qq= "123456"; String regex= "[1-9][0-9]{4,14}"; // Regular Expressions Boolean b = qq.matches (regex); SYSTEM.OUT.PRINTLN (QQ+ ":" +b); // qq:true
Regular expressions are used to manipulate string data.
Embodied by some specific symbols.
In order to master the regular expression, some symbols must be learned.
Although simplified, but poor in reading.
JDK API 1.60 Chinese version
Java.lang String matches Regular expression
Common:
1. Characters
X character X
\ \ backslash Character
2. Character classes
[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)
[A-d[m-p]] A to D or M to P:[a-dm-p] (set)
[A-z&&[def]] D, E or F (intersection)
[A-Z&&[^BC]] A to Z, except B and C:[ad-z] (minus)
[A-z&&[^m-p]] A to Z, not M to P:[a-lq-z] (minus)
3. Predefined character classes
. 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]
4. Boundary Matching Device
^ The beginning of the line
End of the $ line
\b Word boundaries
5.Greedy Quantity 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 no more than m times
public class Regexdemo {
public static void Main (string[] args) {
/*
* Regular expressions are common to strings:
* 1, matching. The
* is actually using the matches method in the string class.
*
* 2, cut. The
* is actually using the split method in the String class.
*
* 3, replace. The
* is actually using the ReplaceAll () method in the String class.
*
* 4, get.
*
*/
Functiondemo_4 ();
}
/*
* Get
* Encapsulates a regular rule object.
* Pattern p = pattern.compile ("A*b"); The
*//is associated with the Matcher method string of the regular object. Gets the Matcher object to be manipulated against the string.
* Matcher m = p.matcher ("Aaaaab");
*//The string is manipulated by the method of the Matcher object.
* Boolean B = m.matches ();
*/
public static void Functiondemo_4 () {
String str = "da jia hao,ming tian Bu fang jia!";
String regex = "\\b[a-z]{3}\\b";
//1 to encapsulate the regular as an object.
Pattern p = pattern.compile (regex);
//2 to get the match object from the regular object.
Matcher m = p.matcher (str);
//Use the method of the Matcher object to manipulate the string.
//If you want to get a three-letter word
//lookup. Find ();
System.out.println (str);
while (M.find ()) {
System.out.println (M.group ());//Gets the matching subsequence
System.out.println (M.start () + ":" +m.end ());
}
}
/*
* Replace
*/
public static void Functiondemo_3 () {
String str = "Zhangsanttttxiaoqiangmmmmmmzhaoliu";
str = Str.replaceall ("(.) \\1+ "," # ");
Replace the overlapping words with #
The output is as follows
/* Zhangsan#xiaoqiang#zhaoliu */
str = Str.replaceall ("(.) \\1+ "," $ ");
$ Gets the first group in the previous parameter
The output is as follows
/* Zhangsantxiaoqiangmzhaoliu */
System.out.println (str);
String Tel = "15800001111";//158****1111;
Tel = Tel.replaceall ("(\\d{3}) \\d{4} (\\d{4})", "$1****$2");
SYSTEM.OUT.PRINTLN (tel);
}
/*
Cutting
*
* Group: ((A) (B (C)))
*/
public static void Functiondemo_2 () {
String str = "Zhangsan xiaoqiang Zhaoliu";
string[] names = Str.split ("+");//output results are as follows
/* Zhangsan
Xiaoqiang
Zhaoliu
*/
String str = "Zhangsan.xiaoqiang.zhaoliu";
string[] names = str.split ("\ \"); /output results are as follows
/* Zhangsan
Xiaoqiang
Zhaoliu
*/
String str = "Zhangsanttttxiaoqiangmmmmmmzhaoliu";
string[] names = Str.split ("(.) \\1+ ");//number represents the first group
for (String name:names) {
SYSTEM.OUT.PRINTLN (name);//output results are as follows
/* Zhangsan
Xiaoqiang
Zhaoliu
*/
}
}
/*
* Demo match.
*/
public static void Functiondemo_1 () {
Match the phone number is correct.
String Tel = "15800001111";
String regex = "1[358]\\d{9}";
Boolean B = tel.matches (regex);
System.out.println (tel+ ":" +b);
}
}
Import Java.util.TreeSet;
public class Regextest {
/**
* @param args
*/
public static void Main (string[] args) {
/*
* 1, treating stuttering: I I ... I-I-I ... I-I-I'm going to ... If you want to ... If you want to. Learn to learn and learn. Learn to learn to edit ... Compilation and compilation: Compilation: Regulation Regulation ... Regulation Regulation ... Cheng
* 2, sort the IP address.
* 3, check the email address.
*/
Test_3 ();
}
/*
* 1, to cure stuttering.
*/
public static void Test_1 () {
String str = "I am ... I-I-I ... I-I-I'm going to ... If you want to ... If you want to. Learn to learn and learn. Learn to learn to edit ... Compilation and compilation: Compilation: Regulation Regulation ... Regulation Regulation ... Cheng ";
1. Remove the string. Replace with.
str = Str.replaceall ("\\.+", "" ");
System.out.println (str);
2, replace the overlapping words.
str = Str.replaceall ("(.) \\1+ "," $ ");
System.out.println (str); /* Output:
* I want to learn to program
*/
}
/*
* IP address sorting.
*
* 192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55
*/
public static void Test_2 () {
String ip_str = "192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55";
1, in order for IP to be compared in string order, as long as the number of bits per segment of IP is the same.
So, fill 0, according to each one need to do more than 0 to supplement. Each paragraph is added two 0.
Ip_str = Ip_str.replaceall ("(\\d+)", "00$1");
System.out.println (IP_STR);
/* Output:
*00192.00168.0010.0034 00127.000.000.001 003.003.003.003 00105.0070.0011.0055
*/
Then each paragraph retains the number 3 bits.
Ip_str = Ip_str.replaceall ("0* (\\d{3})", "$");
System.out.println (IP_STR);
/* Output:
*192.168.010.034 127.000.000.001 003.003.003.003 105.070.011.055
*/
1, the IP address is cut out.
string[] ips = ip_str.split ("+");
treeset<string> ts = new treeset<string> ();
for (String ip:ips) {
SYSTEM.OUT.PRINTLN (IP);
/* Output:
*192.168.10.34
*127.0.0.1
*3.3.3.3
*105.70.11.55
*/
Ts.add (IP);
}
for (String ip:ts) {
SYSTEM.OUT.PRINTLN (IP);
/* Output:
*003.003.003.003
*105.070.011.055
*127.000.000.001
*192.168.010.034
*/
System.out.println (Ip.replaceall ("0* (\\d+)", "$"));/* Output:
*3.3.3.3
*105.70.11.55
*127.0.0.1
*192.168.10.34
*/
}
}
/*
* Check the email address.
*/
public static void Test_3 () {
String mail = "[email protected]";
String regex = "[A-za-z0-9_][email protected][a-za-z0-9]+ (\\.[ a-za-z]{1,3}) + "; Generally do not use this, the Internet has standard
Regex = "\\[email protected]\\w+ (\\.\\w+) +";//[email protected] general-style matching
Boolean B = mail.matches (regex);
System.out.println (mail+ ":" +b);
}
}
/*
* Web crawler: In fact, a program is used to obtain data that conforms to the specified rules on the Internet.
*
* Crawl email address.
*
*/
public class RegexTest2 {
/**
* @param args
* @throws IOException
*/
public static void Main (string[] args) throws IOException {
List<string> List = Getmailsbyweb ();
for (String mail:list) {
System.out.println (mail);
}
}
public static list<string> Getmailsbyweb () throws IOException {
//1, read the source file.
//bufferedreader bufr = new BufferedReader (New FileReader ("c:\\mail.html"));
URL url = new URL ("http://192.168.1.100:8080/myweb/mail.html");
BufferedReader bufin = new BufferedReader (New InputStreamReader (Url.openstream ()));
//2 to match the data being read to a rule. Gets the data that conforms to the rule from.
String Mail_regex = "\\[email protected]\\w+ (\\.\\w+) +";
List<string> List = new arraylist<string> ();
Pattern p = pattern.compile (Mail_regex);
String line = null;
while ((Line=bufin.readline ())!=null) {
Matcher m = p.matcher (line);
while (M.find ()) {
///3, the data that conforms to the rule is stored in the collection.
List.add (M.group ());
}
}
return list;
}
public static list<string> Getmails () throws ioexception{
1. Read the source file.
BufferedReader bufr = new BufferedReader (New FileReader ("c:\\mail.html"));
2. Match the data that is read to the rule. Gets the data that conforms to the rule from.
String Mail_regex = "\\[email protected]\\w+ (\\.\\w+) +";
list<string> list = new arraylist<string> ();
Pattern p = pattern.compile (Mail_regex);
String line = null;
while ((Line=bufr.readline ())!=null) {
Matcher m = p.matcher (line);
while (M.find ()) {
3, the data that conforms to the rule is stored in the collection.
List.add (M.group ());
}
}
return list;
}
}
Java Learning--Regular expressions