Java Learning Series (24) A detailed description of Java regular expressions

Source: Internet
Author: User


Reprint Please specify source:http://blog.csdn.net/lhy_ycu/article/details/45501777

Objective

A regular expression can be said to be used to deal with strings of a sharp weapon, it is a special match n string template, the essence is to find and replace. Look at the two tool classes, pattern: compiled with matching templates (such as: Pattern.compile ("[A-z]{2}"), and//take 2 lowercase letters before the instance presentation. Matcher: The result of matching the target string (for example: Pattern.matcher ("target string");). The string also has a matches method to determine whether the target string matches a given regular expression in the form of: targetstr.matches (regex); The return type is Boolean.


Basic Use Method
(i) supported basic wildcard characters:

. -Can match any character
\s-Represents an arbitrary white space (space, Tab).
\s-Represents an arbitrary non-whitespace.
\d-Represents an arbitrary number (digital).
\d-Represents an arbitrary non-number.
\w-Represents a single word character.
-W-Represents an arbitrary non-word character

Note: For special characters, remember to escape when actually used , such as: () [] {} \? * + ^ (beginning of a line) $ (end of line) |

(ii) Range of values (used as "adverbs" for occurrences)

? --representing the things in front of it can appear 0~1 times
*--represents something in front of it can appear 0~n times
+--represents something in front of it can appear 1~n times
{N,m}--The thing that stands in front of it can appear n~m times
{N,}--represents at least n occurrences of something in front of it
{, M}--represents most of the things in front of it appears m times
{n}--The thing that stands before it must appear n times

(c) square brackets expression

enumeration: [AB1]--Represents a or B or 1.
Range: [A-c]--represents any character in the a,b,c.
Enumerations and scopes: [a-c1-3]--represents any one character in the a,b,c,1,2,3.
Indicates no: [^a-c]--represents any one of the characters that is not included in the a,b,c.
Expression: [a-g&&[^b-d]]:--represents any one character in the a,e,f,g.
Indicates that one of these must be included: (COM|ORG|CN)

Summary: one character with \, multiple characters with [], character count with {}


Example description

(a) Basic usage demo:

/** * Regular Expression Instance Demo Description * * @author [* Reproduced yesterday *] [email protected] * @since version 1.0 * @datetime May 5, 2015 afternoon 2:27:50 */publ IC class Regextest {public static void main (string[] args) {//single character System.out.println ("a". Matches (".")); /0~1 a aSystem.out.println ("a". Matches ("A?")); /1~n aSystem.out.println ("AAAA". Matches ("A +"));//0~n ASystem.out.println ("". Matches ("A *"));//1~n Q and a 0~ The number System.out.println between 9 ("Qqqqqq3". Matches ("q+[0-9]"));//12~100 number System.out.println ("12345667890123". Matches ( "\\d{12,100}");//0~3 numbers are separated by. System.out.println ("192.168.0.1". Matches ("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{ 1,3} "));//First digit 0~2, second digit 0~9, third number 0~9system.out.println (" 192 ". Matches (" [0-2][0-9][0-9] ");// 4 arbitrary Blank System.out.println ("\n\r\t". Matches ("\\s{4}"));//special character \ need to escape System.out.println ("\ \". Matches ("\\\\");//start with H , with 0~n characters in the middle, and finally ending with o System.out.println ("Hello". Matches ("^.*o$"));//start with H, trailing an O with a letter in the middle, followed by a blank 0~ n characters and ends with D System.out.println ("Hello World". Matches ("^h[a-z]{1,3}o\\b.*d$"));//with any white space and not beginning with a line break, and with a newline ending syStem.out.println ("\ n". Matches ("^[\\s&&[^\\n]]*\\n$"));//0~n characters, connecting 4 digits and one character System.out.println ("AAA 2222q ". Matches (". *\\d{4}. "));}}

(ii) Practical application Demo:

1. Read all the email addresses in the Web page

/** * Read all the email addresses in a webpage--Basic Find * * @author [* Reproduced yesterday *] [email protected] * @since version 1.0 * @datetime May 5, 2015 4:20: XX */public class Emailtest {public static void main (string[] args) {//1~n words (may contain.,-) Connect @1~n words in a row. Finally, com|org|cn|net one of the One end of String emailtemplate = "[\\w[.-]][email protected][\\w]+\\. (com|org|cn|net) "; BufferedReader br = null;try {br = new BufferedReader (new InputStreamReader (New FileInputStream ("d:\\email.html")); String line = null; StringBuffer sb = new StringBuffer (), while (line = Br.readline ())! = null) {Sb.append (line). append ("\ n"); Parse (sb.tostring (), emailtemplate);} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {try { Br.close ();} catch (Exception E2) {//Todo:handle exceptione2.printstacktrace ();}}} /** * Print all e-mail addresses in Web pages * * @param targetstr * Target String * @param template * Regular template to be compiled */public static void P Arse (string targetstr, string template) {if (Targetstr = = NULL | | template = = null) {return;} Gets the compiled template to match pattern pattern = pattern.compile (template);//Gets the result that matches the target string matcher matcher = Pattern.matcher ( TARGETSTR);//If you are looking for the next string that matches the regular expression while (Matcher.find ()) {//Then take out the last character string that matches the regular expression. System.out.println ("=======" + Matcher.group ());}}}

2, the number of lines of code statistics:

/** * Code Statistics: The number of lines of code that traverse a project's source file. * * Includes: number of blank lines, number of lines of code, number of comment lines. * * @author [* Reproduced yesterday *] [email protected] * @since version 1.0 * @datetime May 5, 2015 PM 4:40:12 */public Class Codecount ER {/** * blank line number */private static long whitelines = 0;/** * code line number */private static long normallines = 0;/** * Comment Line number */private static long commentlines = 0;public static void Main (string[] args) {file Srcdir = new File ("D:\\workspace\\android\\abc\ \src "); MyList (srcdir);//traverse the Java source file System.out.println (" Whitelines = "+ Whitelines); System.out.println ("Normallines =" + Normallines); System.out.println ("Commentlines =" + Commentlines); System.out.println ("Totallines =" + Gettotallines ());} /** * Gets the total number of rows */private static long Gettotallines () {Long value = whitelines + normallines + commentlines;return value;} /** * Traverse the Java source file */private static void MyList (file srcdir) {System.out.println (Srcdir + "directory contains directories and sub-files are:"); file[] files = srcdir.listfiles (); for (File file:files) {System.out.println ("----------" + File); if (file.getname(). Matches (". *\\.java$")) {parse (file);} if (File.isdirectory ()) {myList (file);}}} /** * Read Source file contents * * @param file * java file */private static void parse (file file) {BufferedReader br = null;/** * Identifies the start or end of a comment */boolean comment = false;try {br = new BufferedReader (new InputStreamReader (file)); String lines = null;while (line = Br.readline ()) = null) {line = Line.trim ();//with any whitespace and not beginning with a newline, and ending with a newline if (line.matches ("^ [\\s&&[^\\n]]*$]) {whitelines++;} else if (Line.startswith ("/*")) {commentlines++;comment = true;} else if ( Comment = = true) {commentlines++;if (Line.endswith ("*/")) {comment = false;}} else if (Line.contains ("//")) {Commentlines ++;} else {normallines++;}}} catch (IOException e) {e.printstacktrace ();} finally {if (BR! = null) {try {br.close (); br = null;} catch (IOException e) { E.printstacktrace ();}}}}

(c) Regular expression advanced use:

Find a child string

String S1 = "123-45678-987-11"; Pattern pattern = Pattern.compile ("\\d{3,5}"); Match five numbers Matcher matcher = Pattern.matcher (S1); System.out.println (Matcher.matches ());//Falsematcher.reset ();//Reset the match, set its add position to zero System.out.println (matcher.find ());//True, because the match is reset, the System.out.println (Matcher.start () + "-" + matcher.end ()) is found from the starting position;//Location: 0-3// The only difference with the matches method is that Lookingat does not need to match the entire area, it is always starting from the first substring System.out.println (Matcher.lookingat ());// TrueSystem.out.println (Matcher.lookingat ());//True

Find and replace

Case_insensitive: Ignore substring case Pattern pattern2 = pattern.compile ("java", pattern.case_insensitive); Matcher matcher2 = Pattern2.matcher (Java java java Iloveyoujava Youhatejava);//Replace all found substrings (find and replace) SYSTEM.O Ut.println (Matcher2.replaceall ("JAVA"));

Conclusion
About the basic use of the normal expression in Java almost all of these, of course, the role of regular expressions far more than these, as for the deeper application later encountered will be updated up. As of now, the Javase Foundation is almost here, and later will update Java performance optimization, in-depth understanding of the JVM, Android Learning series, etc., please look forward to!


Java Learning Series (24) A detailed description of Java 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.