Dark Horse Programmer----Java Basics: Regular expressions

Source: Internet
Author: User
Tags character classes ming

-------Android Training, Java training, look forward to communicating with you! ----------

Regular Expressions: expressions that conform to certain rules, with specific symbols to express some code.

Features: Used for some specific symbols to represent some code manipulation, which simplifies writing

Function: specifically for manipulating strings.

Pros: You can simplify complex operations on strings.

Cons: The more symbol definitions, the longer the regular expression, the worse the reading

A summary of the construction of regular expressions

1. Characters

\ \ backslash Character

\ t tab (' \u0009 ')

\ n New Line (newline) character (' \u000a ')

\ r return character (' \u000d ')

2. 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)

[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]

\w Word character: [a-za-z_0-9]

\w non-word characters: [^\w]

4. Boundary Matching Device

\b Word boundaries

\b Non-word boundaries

5, 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 no more than m times

6. Group and capture

capturing groups can be numbered by calculating their opening brackets from left to right. For example, in an expression ((A) (B (C))), there are four such groups:

1 ((A) (B (C)))

2 \a

3 (B (C))

4 (C)

Group 0 always represents an entire expression. The contents of the matching group are commonly used in replacements.

Specific operating functions: matching, cutting, replacing and acquiring

match: String matches method .

Matches the entire string with a rule, so long as there is a non-conforming rule, the match ends and false is returned.


Determine whether a serial number is a mobile phone number: Mobile phone number segment only 13xxx 15xxx 18xxxx

public static void Checktel ()

{

String Tel = "16900001111";

String Telreg = "1[358]\\d{9}";

System.out.println (Tel.matches (Telreg));

}

Determines whether a string begins with a letter and the rest is full of numbers.

public static void Demo ()

{

String str = "b23a23456789";

String reg = "[a-za-z]\\d*";

Boolean b= str.matches (REG);

System.out.println (b);

}

Determine whether QQ is correct: qq5-15 bit, can not start with 0, the middle can not have letters

public static void Checkqq ()

{

String QQ = "123a454";

String regex = "[1-9]\\d{4,14}";

Boolean flag = Qq.matches (regex);

if (flag)

System.out.println (qq+ "... is ok");

Else

System.out.println (qq+ "... Illegal ");

}

Cut: String split ();

Multi-space cut:"_+" _ Denotes spaces

. Point of Cut:"\." Operation

\ \ Cut:"\\\\" Operation

Cutting of overlapping words:"(.) \\1+ "to operate, where (.) is a wildcard character, \\1 represents a stack, and the following + represents multiple folds.

Example: cutting c:\\abc\\234\\a.doc with \ \

Class Regexdemo

{

public static void Main (string[] args)

{

Splitdemo ("C:\\abc\\234\\a.doc", "\\\\");

}

public static void Splitdemo (String s,string t)

{

String [] arr= s.split (t);

for (String St:arr)

System.out.println (ST);

}

}

replacement :String replaceall (REGEX,STR); If there is a defined group in the regex, you can get the existing group in the regular expression in the second argument through the $ symbol.

typical notation Str.replaceall ("(.) \\1+ "," $ ")

Instance:

public static void Test_1 ()

{

String str = "I am ... I-I-I ... I'm going to.. Want to ... Want to ... Learn to learn .... Learn to learn ... To make a series of ... Programming.. Cheng Regulation Regulation ... Ride... Process ";

/*

Changes an existing string to another string. Use the Replace function.

1, you can first. Remove.

2, when multiple duplicate content becomes a single content.

*/

str = Str.replaceall ("\\.+", "" ");

System.out.println (str);

str = Str.replaceall ("(.) \\1+ "," $ ");

System.out.println (str);

}

get : Takes out a substring of a string that conforms to the rules.

Operation Steps:

1, encapsulates the regular expression into an object. Pattern p = pattern.compile (reg);

2 to associate the regular object with the string to manipulate. Matcher m = p.matcher (str);

3, after correlation, gets the regular match engine. While (M.find ())

4. Operate the rules-compliant substring through the engine, {M.group ();} Pseudo Code

Like taking out.


Example: Get Ming Tian Jiu Yao Fang Jia le, da Jia. 4 Letters in a word

Import java.util.regex.*;

Class RegexDemo2

{

public static void Main (string[] args)

{

Getdemo ();

}

public static void Getdemo ()

{

String str = "Ming Tian Jiu Yao Fang Jia le, da Jia. ";

System.out.println (str);

String reg = "\\b[a-z]{4}\\b";

Encapsulates a rule as an object.

Pattern p = pattern.compile (reg);

Associates the regular object with the string to be played. Gets the match object.

Matcher m = p.matcher (str);

while (M.find ())

{

System.out.println (M.group ());

System.out.println (M.start () + "...." +m.end ());//return to index position

}

}

}

where group (), Start (), and end () take the parameter I is the sub-expression index in the regular expression (the first few sub-expressions)

Summary of Usage:

1. Just want to know the matching of the string right and wrong (judging)

2. Change the string to another string with the replace

3. To get a string by rule to multiple substrings with split

4. Want to get a part of a string with get


Web crawler Examples:

/*

Web crawler (spider)

is actually a feature that collects specified information on the network

Requirements: can be used to collect information such as mailboxes.

Applications: such as keyword search, the actual use of "spider", by looking for keywords to obtain relevant information

*/

Import java.io.*;

Import java.util.regex.*;

Import java.net.*;

Import java.util.*;

Class RegexTest2

{

public static void Main (string[] args) throws Exception

{

Getmails_1 ();

}

public static void Getmails_1 () throws Exception

{

URL url = new URL ("http://192.168.1.254:8080/myweb/mail.html");

URLConnection conn = Url.openconnection ();

BufferedReader Bufin = new BufferedReader (New InputStreamReader (Conn.getinputstream ()));

String line = null;

String Mailreg = "\\[email protected]\\w+ (\\.\\w+) +";

Pattern p = pattern.compile (Mailreg);

while ((Line=bufin.readline ())!=null)

{

Matcher m = p.matcher (line);

while (M.find ())

{

System.out.println (M.group ());

}

}

}

/*

Gets the e-mail address in the specified document.

Use the Get feature. Pattern Matcher

*/

public static void Getmails () throws Exception

{

BufferedReader BUFR =

New BufferedReader (New FileReader ("Mail.txt"));

String line = null;

String Mailreg = "\\[email protected]\\w+ (\\.\\w+) +";

Pattern p = pattern.compile (Mailreg);

while ((Line=bufr.readline ())!=null)

{

Matcher m = p.matcher (line);

while (M.find ())

{

System.out.println (M.group ());

}

}

}

}

This article is from the "Dot Drop" blog, please be sure to keep this source http://arctictern.blog.51cto.com/10120640/1660008

Dark Horse Programmer----Java Basics: Regular expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.