A detailed description of the Java regular expression (iii)

Source: Internet
Author: User

The regular expression engine is a software that can handle regular expressions. Many languages support regular expressions for working with text, such as PHP, Java, and JavaScript, and some text editors implement advanced search-and-replace functionality with regular expressions. The addition of the Java.util.regex package in Java provides support for regular expressions, and the functions in the Java.lang.String class are also called regular expressions. Next, we will explain the use of two aspects

    • Use the match function of the string class: match, Split, and replace

    • Using the regular expression Library Java.util.reg: Find, extract, split, replace




1, using the matching function of the string class

There are 4 methods in a string that can use regular expressions, which are matches (), Split (), ReplaceAll (), and Replacefirst ().

The matches () method can determine whether the current string matches a given regular expression.

Public boolean matches (String regex);

The split () method separates the string and returns the result as a string array.

Public string[] Split (string regex);p ublic string[] Split (string regex,int limit);

ReplaceAll () and Replacefirst () methods

public string ReplaceAll (string regex,string replacement);p ublic string Replacefirst (string regex,string replacement);

2. Use regular expression library pattern and Matcher

The JDK's regular expression library Java.util.reg provides two classes of pattern and matcher for regular expression matching and lookup functions.

(1) Pattern expression

Pattern is a regular expression of a string that creates a pattern directly based on a regular expression.

Pattern p = pattern.compile ("A*b");

(2) Matcher matching device

Use the Pattern object to get the Matcher match object, and then create a match that you can use to perform different matching operations.

    • The matches () method attempts to match the entire input sequence to the pattern

    • The Lookingat () method attempts to match the input sequence from the beginning to the pattern

    • The Find () method scans the input sequence to find the next sub-sequence that matches the pattern

3.4 functions in regular expression library

Regular expressions have powerful functions in handling strings, mainly using Matcher's matching functions, which are commonly used in 4.

    • Query---find ()

    • Extract---Group ()

    • Split---split ()

    • Replace---replaceall ()

Examples are as follows:

/** *  gets the text value of a node in the XML text  *  @param  xml  find XML text  *  @param  nodename   Node name  *  *  @return   text value of the target node  */public static String  Getnodetext (String xml, string nodename)  {String regex =  "< ("  +  nodeName +  ") > ([^<>/]*) </\\1>"; Matcher m = pattern.compile (Regex, pattern.dotall). Matcher (XML);if  (M.find ())  { Return m.group (2);}  else {return null;}} /** *  get text values for a node in soap-xml text  *  @param  xml  find XML text  *  @param   nodename  node name  *  *  @return   text value of the target node  */public static String  Getsoapnodetext (String xml, string nodename,string prefix)  {nodeName=prefix+ ":" + NodeName; string regex =  "< ("  + nodeName +  ") > ([^<>/]*) </\\1>"; matcher m =&nbsp Pattern.compile (Regex, pattern.dotall). Matcher (XML);if  (M.find ())  {return m.group (2);}  else {return null;}} /** *  gets the text value of a node in the XML text  *  @param  xml  find XML text  *  @param  nodename   Node name  *  *  @return   text value of the target node  */public static String  Getnodetextbynum (String xml, string nodename, int num)  {String regex =   "< ("  + nodeName +  ") > ([^<>/]*) </\\1>"; Matcher m = pattern.compile (Regex, pattern.dotall). Matcher (XML);int i = 1; while  (M.find ())  {if  (I++ == num)  {return m.group (2);} Return null;} /** *  gets the text value of all nodes when a node in the XML is duplicated  *  @param  xml  find XML text  *  @param   nodename  node name  *  *  @return   text value of the target node  */public static List< String> getnodetExtlist (String xml, string nodename)  {List<String> list=new ArrayList< String> (); string regex =  "< ("  + nodeName +  ") > ([^<>/]*) </\\1>"; Matcher m = pattern.compile (Regex, pattern.dotall). Matcher (XML);while  (M.find ())  {list.add (M.group (2));} Return list;} /** *  get XML text content  *  @param  xml  find XML text  *  *  @return   Newspaper style content  */public static string getxmlbodytext (string xml)  {String regex  =  "<SvcCont> (. +?) </SvcCont> "; Matcher m = pattern.compile (Regex, pattern.dotall). Matcher (XML);if  (M.find ())  { Return m.group (1);}  else {return null;}}



This article is from the "Ah Cool blog source" blog, please make sure to keep this source http://aku28907.blog.51cto.com/5668513/1774276

A detailed description of the Java regular expression (iii)

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.