Basic use of regular expressions in Java

Source: Internet
Author: User

overview, when the manipulation of a string of logic is very complex, the code is large enough to take the form of regular expressions to deal with the string, but there are shortcomings, is to be familiar with the characters, symbols and the meaning of the combination of symbols need to be used frequently to be able to be born skillfully

A Characteristics of regular Expressions

1. function: An expression conforming to a certain rule, used to specialize in manipulating strings

2. Features: used for some specific symbols to represent some code operations

3. Benefits: can simplify the complex operation of the string

Two Matching function

1. definition: string matches method, match the entire string with a rule, as long as there is a non-conforming rule, the match ends, return false

1  public Static voidcheckqq () {2String qq= "123456a";3String regex= "[1-9][0-9]{4,14}";4         Booleanflag=qq.matches (regex);5         if(flag) {6System.out.println (qq+ "is OK ...");7}Else{8System.out.println (qq+ "input error");9         }Ten}
View Code
1  public Static void demo () {2         String str= "b23a23456789"; 3         String reg= "[a-za-z]\\d{2}[a-z]\\d*"; 4         boolean flag=str.matches (reg); 5         System.out.println (flag); 6     }
View Code
This method does not use regular expressions, complex operation, large code size public Static voidcheckqq_1 () {String QQ=""; intlen=Qq.length (); if(len>=5&&len<=15){            if(!qq.startswith ("0")){                Try {                    LongL=Long.parselong (qq); System.out.println ("qq:" +qq); } Catch(Exception E) {//Todo:handle ExceptionSystem.out.println ("illegal characters appear"); }                 /*Char Arr[]=qq.tochararray ();                Boolean flag=false; for (int i = 0; i < arr.length; i++) {if (!) (                        arr[i]>= ' 0 ' &&arr[i]<= ' 9 ')) {flag=false;                    Break                }} if (flag) {System.out.println ("qq:" +qq);                }else{System.out.println ("illegal character"); }*/            }Else{System.out.println ("QQ cannot start with 0"); }        }Else{System.out.println ("length error"); }    }
View Code

Three. Cutting

1.String Sprit ();

1  public Static voidSplitdemo (String str,string reg) {2         //String reg= "+";//to cut by multiple spaces3string[] attr=Str.split (reg);4 System.out.println (attr.length);5          for(String S:attr) {6 System.out.println (s);7         }8}
View Code

Four Replace

1.String ReplaceAll ()

1  public Static void replacealldemo (String str,string regex,string newstr) {2         str=str.replaceall (regex, newstr ); 3         System.out.println (str); 4     }
View Code

Five Get

1. definition: Remove a substring of a string that conforms to a rule

2. Operation procedure

2.1 Encapsulating a regular expression as an object

2.2 To have the regular object associated with the string to manipulate

2.3 After association, get the regular match engine

2.4 Operation of Rules-compliant substrings through the engine, such as removing

1  public Static voidGetdemo () {2String str= "ming Tian Yao Fang Jia Le,da jia. ";3String reg= "\\b[a-z]{3}\\b";//\b Represents the word boundary4         //encapsulate a rule as an object5Pattern p=pattern.compile (reg);6         //to have the regular object associated with the string to be played, get the match object7Matcher m=P.matcher (str);8System.out.println (m.matches ());//in fact, the matches method in the string class is done with the pattern and the Matcher object .9                                         //is simply encapsulated by the method of the string class, it is relatively simple to use, but the function is single .Ten         //Boolean b=m.find ();//to apply a rule to a string, to make a rule-compliant substring lookup one         //System.out.println (b); a         //System.out.println (m.group ());//used to get the result of a match -          while(m.find ()) { - System.out.println (m.group ()); theSystem.out.println (m.start () + "...." +m.end ()); -         } -          -}
View Code

Six Selection of regular expression functions

1. Determine if the string is correct, matching function

2. Change the string to another string, replace the function

3. Turn the string into multiple strings in the specified way, cutting function

4. Get the string substring that meets the requirements, get the function

--------------------------------------technology changes the life, the knowledge changes the destiny!

Basic use of regular expressions in Java

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.