Java Regular Expression exercises

Source: Internet
Author: User

1  PackageShb.java.demo3;2 3 ImportJava.util.regex.Matcher;4 ImportJava.util.regex.Pattern;5 6 /**7 * Regular expression simple operation8 * @Package: Shb.java.demo39 * @Description:Ten  * @authorshaobn One * @Date 2015-8-28 a.m. 10:54:30 A  */ -  Public classRegexDemo2 { -  Public Static voidMain (string[] args) { theIftrue ("[Email protected]", "[0-9]{8,11}@ (\\w*|[ 0-9]+) \ \. [a-z]*]); -Splitregex ("C:\\demo\\txt", "\\\\"); -Iftrue ("ABC bbb", "\\AAbc\\b"); -Iftrue ("131222", "(13|2|3)". { 2,4}2$ "); +Iftrue ("Java", "^j.*a$"); -Iftrue ("Javaa", "java*"); +Iftrue ("aBc123", "[a-za-z]{3}\\d{3,9}"); AIftrue ("13635586954", "[1][3,5,8]\\d{9}"); atIftrue ("13635586954", "[1][3,5,8]\\d*"); -Splitregex ("The Day is Friday", "+"); -Splitregex ("Ttddayiisffriday", "(.) \\1 "); - } - //match a string based on a regular expression -  Public Static voidiftrue (String str,string regex) { inPattern p =pattern.compile (regex); -Matcher m =P.matcher (str); to     BooleanFlag =m.matches (); + System.out.println (flag); - } the //cutting strings based on regular expressions *  Public Static voidSplitregex (String str,string regex) { $string[] STRs =str.split (regex);Panax Notoginseng      for(String s:strs) { - System.out.println (s); the     } +      A } the //cutting strings based on regular expressions +  Public Static voidSplitRegex2 () { -Pattern pattern = Pattern.compile ("[, |] +"); $string[] STRs = Pattern.split ("Java Hello World java,hello,,world| Sun "); $      for(inti=0;i<strs.length;i++) { - System.out.println (Strs[i]); -     }  the } -}

Java Regular Expression exercises

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.