Java Regular Expressions four commonly used processing methods (matching, segmentation, substitution, get) _ Regular expressions

Source: Internet
Author: User

Java Regular Expression Advanced article, introduces four kinds of commonly used processing methods: matching, segmentation, substitution, obtain, the specific contents are as follows

Package test; 
Import Java.util.regex.Matcher; 
  
Import Java.util.regex.Pattern; 
 /** * Regular Expressions The use of regular expressions is mainly 4 kinds of uses * match, split, replace, get. * With some simple symbols to represent the operation of the code * * @author CYC * * * * * * * */public class Rex {public static void main (string[] args) {//for string at 
    Daniel Rex Reg = new Rex (); Check QQ reg Regular Expression//here is the \w refers to [a-za-z0-9], there is an important one, *.+ these three separate//? 1 or 1 times,//+ says 1 or n times,//* says 0 times or 
    n times,//There are some special x{n} exactly n times x{n,} at least n times, x{n,m}n times to M times, String mathreg = "[1-9]\\d{4,19}"; String Divisionreg = "(.) 
    \\1+ "; 
    \\b refers to the boundary value String getstringreg = "\\b\\w{3}\\b"; 
    String matching (first is a string except 0) reg.getmatch ("739295732", Mathreg); 
    Reg.getmatch ("039295732", Mathreg); 
    String substitution//removal stack words reg.getreplace ("12111123ASDASDAAADDD", Divisionreg, "$"); String split//Cut stack, repeat//here to know the concept of a group (.) 
    \\1 the second and first to the same reg.getdivision ("Aadddddasdasdasaaaaaassssfq", Divisionreg); String fetch//now gets three strings out of Reg.getstring ("Ming Tian Jiu Yao Fangjia le ", getstringreg); /** * Get the string of the query * will match the string out/private void getString (String str, string regx) {//1. Encapsulating an expression as an object pa 
    Tten class to achieve pattern pattern = Pattern.compile (REGX); 
    2. Associate a string with a regular expression Matcher Matcher = Pattern.matcher (str); 
      
    The matches method in 3.String objects is achieved through this matcher and pattern. 
    System.out.println (Matcher.matches ()); 
      Look for substring while (Matcher.find ()) {//Get string System.out.println (Matcher.group ()) that matches the rule; 
    Gets the first position and end position of the string System.out.println (Matcher.start () + "--" +matcher.end ()); }/** * String split/private void Getdivision (String str, string regx) {string [] Datastr = Str.spli 
    T (REGX); 
    for (String s:datastr) {System.out.println ("regular expression split + +" +s);  }/** * string substitution/private void getreplace (String str, string regx,string replacestr) {string stri 
    = Str.replaceall (REGX,REPLACESTR); 
  System.out.println ("Regular expression substitution" +stri); 
 } /** * Matching of string processing * Match method in the string class/public void Getmatch (String str, string regx) {System.out.print 
  ln ("Regular expression matching" +str.matches (REGX)); } 
}

The above is the entire contents of the Java regular expression, I hope to be helpful to everyone's study

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.