Learn Java regular expressions (match, replace, find) _java

Source: Internet
Author: User

This article for everyone to share the Java regular expression matching, replacement, find and cut operations, interested friends can refer to the

Import java.util.ArrayList;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern; public class Test {public static void main (string[] args) {getstrings ()/////Get the specified content in the specified string content with a regular expression System.out.pr
    Intln ("********************"); Replace ();
    Replaces the string content with a regular expression System.out.println ("********************"); Strsplit ();
    Use regular expressions to cut string System.out.println ("********************"); Strmatch ();
    String match} private static void Strmatch () {String phone = "13539770000"; Check whether phone is a qualified mobile number (standard: 1 start, second for 3,5,8, 9 digits for any number) System.out.println (phone + ":" + phone.matches ("1[358][0-9]{9,9}"));
    True String str = "ABCD12345EFGHIJKLMN"; Check whether the middle of STR contains 12345 System.out.println (str + ":" + str.matches ("\\w+12345\\w+")); True System.out.println (str + ":" + str.matches ("\\w+123456\\w+")); false} private static void Strsplit () {String str = ' asfasf.sdfsaf.sdfsdfas.asdfasfdasfd.wrqwrwqer.asfsafasf.
    SAFGFDGDSG "; string[] STRs = StR.split ("\.");
    for (String s:strs) {System.out.println (s); The private static void Getstrings () {String str = "rrwerqq84461376qqasfdasdfrrwerqq84461377qqasfdasdaa65464
    5aafrrwerqq84461378qqasfdaa654646aaasdfrrwerqq84461379qqasfdasdfrrwerqq84461376qqasfdasdf "; Pattern p = pattern.compile ("QQ" (. *?)
    QQ ");
    Matcher m = p.matcher (str);
    Arraylist<string> STRs = new arraylist<string> ();      
    while (M.find ()) {Strs.add (M.group (1));
    for (String s:strs) {System.out.println (s);
    The private static void replace () {String str = "ASFAS5FSAF5S4FS6AF.SDAF.ASF.WQRE.QWR.FDSF.ASF.ASF.ASF";
    Replace the. In the string with the _, because. is a special character, so you use \. expression, and because \ is a special character, you use \ n to express it.
    str = Str.replaceall ("\.", "_");    
  System.out.println (str);
 }
}

The focus of the code has been noted for everyone, I hope you can carefully study, summed up learning skills.

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.