4. Familiar with Java Basic Class Library series--java Regular Expression class library

Source: Internet
Author: User

Regular expression Syntax structure diagram:

Java Regular Expression class library structure diagram:

Java Typical example

1. String class matches () method

Determine if a string conforms to a specific regular expression

@Test public void Testregex () {String regex = ". *\\d{3}.*"; String str1 = "11tec34"; String str2 = "285DFFD";  String STR3 = "bac7736db";//Output: False, True, TrueSystem.out.println (Str1.matches (regex) + "," + str2.matches (regex) + "," + Str3.matches (regex));}

2. Split () method of String class

To split a string with regular expressions

@Test public void TestRegex2 () {String input = "Hello3world8my9name1is5jacket"; String regex = "\\d";  With a number as the delimiter string strs[] = Input.split (regex);//output: Hello World My Name is jacketfor (String str:strs) {System.out.print (St R + "");} SYSTEM.OUT.PRINTLN (); input = "Hellodogworlddogmydognamedogisdogjacket"; regex = "dog";  With "dog" character as delimiter STRs = Input.split (regex);//output: Hello World My Name is jacketfor (String str:strs) {System.out.print (str + ") ");} }

3. The string class replace () method

Replacing characters with regular expressions

@Test public void TestRegex3 () {String input = "Hello3world8my9name1is5jacket"; String regex = "\\d";  With a number as the delimiter string firststr = Input.replacefirst (Regex, ":"); Replace only the first matching character string allstr = Input.replaceall (Regex, ":");  Replace all matching characters//output://hello:world8my9name1is5jacket//hello:world:my:name:is:jacketsystem.out.println (FIRSTSTR); System.out.println (ALLSTR);}

4. Pattern.matches () static method

Determines whether a string conforms to a regular expression, equivalent to the matches () method of the String class

@Test public void testRegex4 () {String regex1 = "\\bdog\\b"; String regex2 = ". *\\bdog\\b.*"; String input = "Dog dog dog Doggie Dogg";//output: False,truesystem.out.println (pattern.matches (regex1, input) + "," + Pattern. Matches (regex2, input));  }

5, Pattern class split () method

Splits a string according to a regular expression, equivalent to the split () method of the String class

6.split () Split string  -equivalent to the split () method of string @test public void testRegex6 () {string regex = "\\d"; String input = "Hello3world5hello8java"; Pattern p = pattern.compile (regex); String strs[] = p.split (input);//output: Hello World Hello java for (String str:strs) {System.out.print (str + "");}}

6. Pattern.quote () static method

Gets a regular expression that matches this string

@Test public void testRegex6 () {///example 1String input = "."; String regex = pattern.quote (Input), Boolean flag = pattern.matches (regex, input);//output: \q353434\ EtrueSystem.out.println (regex); SYSTEM.OUT.PRINTLN (flag);//Example 2input = "Test.test"; String erroroutput = Input.replaceall (".", "/");//output://///////system.out.println (erroroutput);//Example 3String Correctoutput = Input.replaceall (Pattern.quote ("."), "/");//output: Test/testsystem.out.println (correctoutput);}

7. Matcher class Find () method, Start () method, End () method

@Test public void testRegex7 () {//m.start ()   m.end ()  String regex = "\\bdog\\b"; String input = "Dog dog dog Doggie Dogg"; Pattern p = pattern.compile (regex);     Matcher m = p.matcher (input);        Gets the match object     int count = 0;     while (M.find ()) {            count++;            Match number 1,start (): 0,end (): 3           //match number 2,start (): 4,end (): 7           //match number 3,start (): 8,end (): 11
   system.out.print ("Match number" + count);            System.out.print (", Start ():" + M.start ());            System.out.println (", End ():" + m.end ());}     }

8. Matcher class Find (int index) method, start (int index), end (int index )

@Test public void testRegex8 () {//m.start () m.end () String regex = "\\bdog\\b"; String input = "Dog dog dog Doggie Dogg";       Pattern p = pattern.compile (regex);        Matcher m = p.matcher (input);  Get the Match object//m.find (int start)//output: Start (): 0,end (): 3//start (): 4,end (): 7 m.find (0);     Start looking for System.out.print ("Start ():" + M.start ()) from the first character of the string to match;     System.out.println ("End ():" + m.end ());    M.find ();     System.out.print ("Start ():" + M.start ());         System.out.println ("End ():" + m.end ());      M.find (int start)//output: Start (): 4,end (): 7//start (): 8,end (): M.find (4);     System.out.print ("Start ():" + M.start ());     System.out.println ("End ():" + m.end ());    M.find ();     System.out.print ("Start ():" + M.start ());         System.out.println ("End ():" + m.end ());    M.start (int group) m.end (int group) m.find (0);//int firststartgroupindex = M.start (0); System.out.println (Firststartgroupindex); }

9. Matcher class Lookingat () method, Matches () method

Determines whether a string matches a specific regular expression, the Lookingat () method does not require a match, and the matches () method requires all matching

@Test public void testRegex9 () {String regex = "Foo"; String str = "fooooooooooooooooo";         Pattern pattern = pattern.compile (regex);         Matcher Matcher = Pattern.matcher (str);          System.out.println ("Current regex is:" + regex);         SYSTEM.OUT.PRINTLN ("Current STR is:" + str);          System.out.println ("Lookingat ():" + Matcher.lookingat ());  True         System.out.println ("matches ():" + matcher.matches ()); False, because matches requires all matches}

There are other methods of matcher that have not been sorted out,

There are other methods of the Matcher class that are not organized:

1.group (int Group) method

2.appendReplacement ()/Appendtail ()/ReplaceAll ()/Replacefirst ()/quotereplacement ()

The Find () method of the 99.Matcher class, the Start () method, and the end () method

  

More detailed regular expression posts can be found in:

Official Microsoft Documentation: https://msdn.microsoft.com/zh-cn/library/ae5bf541 (v=vs.80). aspx

A good regular expression tutorial: Http://www.java3z.com/cwbwebhome/article/article8/Regex/Java.Regex.Tutorial.html#reg7

4. Familiar with Java Basic Class Library series--java Regular Expression class library

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.