Getting started with Java regular expressions

Source: Internet
Author: User

1. What is a regular expression 2. The basis of regular expressions

2.1 Meta characters

2.2 []– square brackets
Brackets enclose a number of characters to represent a metacharacters, which used by itself represents any one of the characters in the square brackets.
Reg= "[abc]4" then "A4", "B4", "C4", are all strings that match this regular expression
[^456]-– stands for any character other than 4,5,6
[A-r] ——-represents any one of the letters in A~R
[a-za-z]--stands for any one English letter
[a-e[g-z]]--represents any one of the letters in A~e or g~z
[a-o&&[def]]--represents the letter d,e,f (intersection operation)
[a-o&&[^bc]]--represents the letter a,d (difference operation)

2.3 () – Brackets
The expression that is enclosed in () is defined as a group, and the character that matches the expression is saved to a staging area, which is useful when the string is extracted.

2.4 Use the qualifier modifier to limit the number of occurrences of metacharacters

3. Some examples of regular expressions
Import Java.util.regex.matcher;import Java.util.regex.pattern;import java.util.regex.PatternSyntaxException; Public classRegular { Public StaticString regexisdigital ="\\d+";//Whether the string is a number       Public StaticString Regexisalpha ="\\p{alpha}+";//Whether the string is a letter       Public StaticString regexisid="\\d{15}|\\d{18}";//Whether the string is a 15-bit or 18-bit body card number        Public StaticString regexisjjjstartkkkend="^jjj.*kkk$";//Whether the string is JJJ beginning KKK end        Public StaticString Regexistelephonenumber ="[0-9]{3,4}\\-?] [0-9]+];//Whether the string is an area code of 3 or 4 digits-(can have no) number of arbitrarily many digits        Public StaticString Regexisemail ="\\[email protected]\\w+ (\\.\\w{2,3}) *\\.\\w{2,3}";//[email protected]        Public StaticString regexishanzi="[\u4e00-\u9fa5]{1,}";//Whether the string is a Chinese character        Public StaticString Regexisstartjava ="^java.*";//Find a string that starts with Java and ends at any end        Public StaticString Regexsplit ="[, |] +";//When splitting strings in multiple conditions        Public Static void Main(string[] args)              {testisdigit ();              Testisalpha ();              Testisid ();              Teststartend ();              Testistelephonenum ();              Testemailaddre ();                        Testishanzi ();              Testisstartjava ();                           Testsplit ();              Testreplace ();         Testpattern (); }Private Static void Testpattern() {Pattern pattern = pattern.compile (""); System. out. println (Pattern.matches ("[0-9]{6}","200038")); System. out. println (Pattern.matches ("\\d{6}","200038")); }Private Static void Testreplace() {Pattern pattern = pattern.compile ("Regular expression"); Matcher Matcher = Pattern.matcher ("Regular expression Hello world, regular expression Hello World");//Replace the first data that matches the regular oneSystem. out. println (Matcher.replacefirst ("Java")); Pattern = Pattern.compile ("Regular expression"); Matcher = Pattern.matcher ("Regular expression Hello world, regular expression Hello World");//Replace all regular-compliant dataSystem. out. println (Matcher.replaceall ("Java")); }Private Static void Testsplit() {Pattern pattern = pattern.compile (regexsplit); String data ="Java Hello World java,hello,,world| Sun "; System. out. println (data+"Split by:"+regexsplit); string[] STRs = pattern.split (data); for(intI=0; i<strs.length;i++) {System. out. println (Strs[i]); }    }Private Static void Testisstartjava() {Pressinfobypattern ("Java is not human", Regexisstartjava); Pressinfobypattern ("Djava is not human.", Regexisstartjava); }Private Static void Testishanzi() {Pressinfo ("Validating Chinese Characters", Regexishanzi); }Private Static void Testistelephonenum() {Pressinfo ("0733-5544", Regexistelephonenumber); Pressinfo ("073-566544", Regexistelephonenumber); Pressinfo ("073566544", Regexistelephonenumber); Pressinfo ("073--566544", Regexistelephonenumber); }Private Static void Teststartend() {Pressinfo ("JJJKKK", regexisjjjstartkkkend); Pressinfo ("JJJAAAAKKK", regexisjjjstartkkkend); Pressinfo ("JJAAAAKKK", regexisjjjstartkkkend); Pressinfo ("Jjjaaaakk", regexisjjjstartkkkend); }Private Static void Testisid() {Pressinfo ("111111111111111", Regexisid); Pressinfo ("11111111111111", Regexisid); Pressinfo ("111111111111122221", Regexisid); Pressinfo ("11111111111112222", Regexisid); }Private Static void Testisalpha() {Pressinfo ("Da", Regexisalpha); Pressinfo ("D1A", Regexisalpha); }Private Static void Testisdigit() {Pressinfo ("21452", regexisdigital); Pressinfo ("21a452", regexisdigital); Pressinfo ("021452", regexisdigital); }Private Static void Testemailaddre() {Pressinfo ("[email protected]", Regexisemail); Pressinfo ("AAAAAAAA", Regexisemail); Pressinfo ("[email protected]", Regexisemail); }Private Static void Pressinfo(String data,string regex) {if(Data.matches (Regex)) {System. out. println (data+": Is match regex:"+regex); }Else{System. out. println (data+": Is not match regex:"+regex); }      }Private Static void Pressinfobypattern(String data,string regex)       {Pattern pattern = pattern.compile (regex); Matcher Matcher = pattern.matcher (data);if(Matcher.matches ()) {System. out. println (data+": Is match regex:"+regex); }Else{System. out. println (data+": Is not match regex:"+regex); }    }}

Getting started with Java 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.