Regular Expressions are rules used to operate strings.
1. In the String class, some methods are used to match and cut strings.
Boolean matches (String regex );
String [] split (String regex) that is cut by the given regular expression );
Replace the String that matches the regular expression with the other String we want: String replaceAll (String regex, String replacement)
2. The following describes common regular expressions.
(1)
Copy codeThe Code is as follows: String regex = "[1-9] [0-9] {4, 15 }";
// [1-9] indicates that this number can only be selected from 1 to 9
// [0-9] indicates that the number can be 0-9
// {} Indicates that the number in the preceding format can be repeated 4-15 times.
This regular expression means that the first number should be any one from 1 to 9, and then one of the numbers in 0-9 must appear, and this number must appear at least four times, up to 15 times
For example:
10175 compliance
10 does not match, because [0-9] {} appears at least four times, only once here
(2)
[A-zA-Z0-9 _] {6} represents exactly 6 Characters in a-z or A-Z or _
+ Indicates at least once
* Indicates zero or multiple occurrences.
? Indicates one or zero occurrence.
(3) Cutting strings based on regular expressions
Copy codeThe Code is as follows: String str = "sjd. ksdj. skdjf ";
String regex = "\\.";
Note: In a regular expression, it is an arbitrary table character and a special symbol. If we want to use. To cut, we must convert it to a common character and use.
Because \ is a special symbol, it must be expressed by two \ characters. When we want to use common \, we need to use \ to represent it.
String [] ss = str. split (regex); returns a String array: "sjd" "ksdj" "skdjf" to cut the original String
(4) replace what we want to replace according to the regular expression.
Replace five or more numeric strings in the string #
Copy codeThe Code is as follows: String str = "abcd1334546lasjdfldsf2343424sdj ";
String regex = "[0-9] {5 ,}";
String newstr = str. replaceAll (regex ,"#");
(5) Obtain strings that comply with regular expression rules
Copy codeThe Code is as follows: Pattern p = Pattern. compile (String regex );
Matcher m = p. matcher (String str );
While (m. find ())
{
System. out. println (m. group ());
}
3. Web Crawler Creation
You can read all the mailboxes on a web page and store them in a text file.
Copy codeThe Code is as follows :/*
Web Crawler
That is, obtain the string or content that matches the regular expression from the webpage.
Get email address from Network
*/
Import java. io .*;
Import java. util. regex .*;
Import java.net .*;
Class MailTest
{
Public static void main (String [] args) throws Exception
{
GetMailAddr ();
}
Public static void getMailAddr () throws Exception
{
URL url = new URL ("http://bbs.jb51.net/topics/390148495 ");
URLConnection con = url. openConnection ();
BufferedReader bufIn = new BufferedReader (new InputStreamReader (con. getInputStream ()));
BufferedWriter bufw = new BufferedWriter (new FileWriter (new File ("e: // mailaddress.txt ")));
String str = null;
String regex = "[a-zA-Z0-9 _] {6, 12} @ [a-zA-Z0-9] + (\. [a-zA-Z] +) + ";
Pattern p = Pattern. compile (regex );
While (str = bufIn. readLine ())! = Null)
{
Matcher m = p. matcher (str );
While (m. find ())
{
String ss = m. group ();
Bufw. write (ss, 0, ss. length ());
Bufw. newLine ();
Bufw. flush ();
}
}
}
}