Java Basics-Regular expressions

Source: Internet
Author: User

. Any character

\d A Digit 0-9

\d a non-digit [^0-9]

\s a whitespace character, white space character

\s a non-whitespace character

\w a word character [a-za-z0-9]

\w a Non-word character

Package Dao;import Java.util.regex.matcher;import Java.util.regex.pattern;public class Testreg {public static void main  (string[] args) throws Exception{/*system.out.println ("abc". Matches ("...");  True ctrl+shift+/comment System.out.println ("a898938593a". ReplaceAll ("\\d", "-"));    A---------Apattern p = pattern.compile ("[A-z]{3}"); Matches a string with 3 characters matcher m = P.matcher ("abc");          System.out.println (M.matches ()); true*///preliminary understanding. * +?, Meta characters/*p ("a". Matches (".")); True P ("AA". Matches ("AA"));   True P ("AAAA". Matches ("A *")); True 0 or NP ("AAAA". Matches ("A +")); True 1 or NP ("AAAA". Matches ("A?"));    False 0 or 1p ("". Matches ("*")); True 0 or NP ("". Matches ("A +"));    True 1 or NP ("". Matches ("A?"));    True 0 or 1p ("215454545454545". Matches ("\\d{3,100}"));           True 3~100 a P ("192.168.000.111". Matches ("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));  TRUEP ("192". Matches ("[0-2][0-9][0-9]"));       true*///Range/*p ("a". Matches ("[ABC]")); True [] takes out aCharacter P ("a". Matches ("[^ABC]"));       False [] takes out a character p ("a". Matches ("[A-za-z]")); True [] takes out a character p ("a". Matches ("[a-z]|[       A-z] "));       True [] takes out a character p ("a". Matches ("[A-z[a-z]")); True [] takes out a character p ("R". Matches ("[a-z&&|[       RFG]]);  True [] Take out a character *///recognize \s \w \d/*p ("\n\r\t". Matches ("\\s{4}"));   True 4 whitespace characters P ("". Matches ("S"));  False S stands for non-whitespace characters p ("A_8". Matches ("\\w{3}"));  TRUEP ("abc888&^%". Matches ("[a-z]{1,3}\\d+[&^$%]+"));  True 1-3 letters, 1 or n digits, [&^$%] any 1 or n p ("\ \". Matches ("\\\\"));  In regular expressions, 2 \ \ Only represents an escape character *///posix Style//p ("a". Matches ("\\p{lower}"));//boundary/*p ("Hello Sir". Matches ("^h.*"));  Truep ("Hello Sir". Matches (". *ir$"));  Truep ("Hello Sir". Matches ("^h[a-z]{1,3}o\\b.*"));  TRUEP ("Hellosir". Matches ("^h[a-z]{1,3}o\\b.*"));  True \b is a space, newline, and other word boundaries *///white lines blank lines//p ("\ n". Matches ("^[\\s&&[^\\n]]*\\n$"));  True begins with a whitespace character, and is not a carriage return, 0 or n times, followed by a newline character//practice/*p ("AAA 8888c". Matches (". *\\d{4}.")); True 0 or n letters, 4 digits,Then there is a letter P ("AAA 8888c". Matches (". *\\b\\d{4}."));  True 0 or n letters, 4 digits preceded by a word boundary, and then there is a letter P ("aaa8888c". Matches (". *\\d{4}."));  True 0 or n letters, 4 digits, and then there is a letter P ("aaa8888c". Matches (". *\\b\\d{4}.")); False 0 or n letters, 4 digits before the word boundary, and then there is a letter *///email//p ("[email protected]". Matches ("[\\w[.-]][email  Protected][\\w[.-]]+\\.  [\\w]+ ")); True//matches find Lookingat start end matches each time you find the entire string, find a string, lookingat each time to find/*pattern p = pattern.compile (" \\d{3,5} "); String s = "123-34345-234-00"; Matcher m = P.matcher (s);p(m.matches ()); Put 123-to eat, so to reset the following.      Otherwise, if not reset, then only 2 true 34345 234m.reset ();  To restore the characters eaten by matches to P (M.find ());  True to find a matching string 123p (M.start () + "-" +m.end ()); 0-3p (M.find ());  True to find a matching string 34345p (M.start () + "-" +m.end ());  4-9p (M.find ());//true find a matching string 234p (M.start () + "-" +m.end ());  10-13p (M.find ());  False 00p (M.lookingat ());  True only starts from head, so it is 123p (M.lookingat ());  True only starts from head, so it is 123p (M.lookingat ());  True only starts from head, so it is 123p (M.lookingat ());True only starts from head, so it is 123*///replacementpattern p = pattern.compile ("java", pattern.case_insensitive); Matcher m = P.matcher (Java java java Ilovejava you hatejava);  StringBuffer buf = new StringBuffer (); int i=0;/*while (M.find ()) {p (M.group ());   List all Java words in}*/while (M.find ()) {i++;if (i%2==0) {m.appendreplacement (buf, "Java");  The even numbers are java}else{m.appendreplacement (buf, "JAVA");//Odd are Java}}m.appendtail (BUF);p(buf); Lists all Java words for}public static void P (Object o) {System.out.println (o);}}

  

Java Basics-Regular expressions

Related Article

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.