Java Learning Diary NUM12

Source: Internet
Author: User

Learning content: An overview of regular expressions. The matching of regular expressions; cut; replace; getI. Overview of regular ExpressionsRegular expression: An expression that conforms to a certain rule. function: Used to specialize in manipulating strings. features: Used for some specific symbols to represent some code manipulation. This simplifies writing. so learning regular expressions is the use of learning some special symbols.  Benefit: You can simplify complex operations on strings. Cons: The more symbol definition, the longer the regular, the worse the reading.  Second, the regular expression of matching, cutting, replacement. 1, matching:String matches method. Matches the entire string with a rule, so long as there is a non-conforming rule, the match ends and false is returned. for the match matches () method, returns True or false based on the rule to determine whether the defined rule is satisfied 2, Cut:String split ();according to the rules, the string is cut to a number of substrings, returning an array of type string 3, replace:String replaceall (REGEX,STR), if there is a defined group in the regex, you can get the existing group in the regular expression in the second argument through the $ symbol. replace some characters or substrings in the source string according to the rulesAn example:class Regexdemo{Public static void Main (string[] args){ Checktel (); //splitdemo ("Erkktyqqquizzzzzo", "(.) \\1+ ");//Finish cutting according to the overlapping words. In order for the result of the rule to be weighed//the rule can be encapsulated into a group. complete with (). The appearance of the group is numbered//starting from 1. If you want to use an existing group, you can get it in the form of \ n (the number of the group). string str = "wer1389980000ty1234564uiod234345675f";//replace the array in the string with #. //replacealldemo (str, "\\d{5,}", "#");String str1 = "Erkktyqqquizzzzzo";//Replace the overlapping words with $. Replaces overlapping characters with a single letter. Zzzz->zReplacealldemo (str1, "(.) \\1+ "," $ ");}Public static void Replacealldemo (String str,string reg,string newstr){str = str.replaceall (REG,NEWSTR); System.out.println (str);}Public static void Splitdemo (String str,string reg){//string reg = "+";//cut by multiple spacesstring[] arr = Str.split (reg); System.out.println (arr.length);For (String S:arr){System.out.println (s);}}4, Get featuresOperation steps:1, encapsulates the regular expression into an object. 2 to associate the regular object with the string to manipulate. 3, after correlation, gets the regular match engine. 4, by the engine to the rules of the sub-string operation, such as remove. An example:Public static void Getdemo (){String str = "Ming Tian Jiu Yao Fang Jia le, da Jia. ";System.out.println (str);String reg = "\\b[a-z]{4}\\b";//Encapsulates the rule as an object. Pattern p = pattern.compile (reg);//Jeang Jong The object is associated with the string to be played. Gets the match object. Matcher m = p.matcher (str);While (M.find ()){System.out.println (M.group ());System.out.println (M.start () + "...." +m.end ());}}

Java Learning Diary NUM12

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.