Regular Expressions General usage

Source: Internet
Author: User

Regular expressions are used in the general way:Pattern: The compiled representation of the regular expression.    That is, each pattern object represents a rule! There is no construction method, and the following method is used to return the Pettern object public static pattern compile (String regex) to compile the given regular expression into the pattern. Creates a corresponding pattern object by the given string form public Matcher Matcher (charsequence input) uses the pattern object to match the string to be matched, returning a match objectThe public Boolean matches () attempts to match the entire region to the pattern. The public boolean find () tries to find the next subsequence of the input sequence that matches the pattern.  Whether there is content in the string that matches the matching rule the public string group () returns the input subsequence that was matched by the previous match operation. Returns the contents of the matching rule in the string to the public int end () returns the trailing index that matches to the content public int start () returns the initial index matching to the content regular use steps: 1:pattern p = Pattern.comp  Ile ("A*b");  Use rules to generate pattern objects 2:matcher m = P.matcher ("Aaaaab");  The Matcher method that calls the pattern object matches the string to match to return the match object (matching engine) 3:boolean B = m.matches (); Call the method of the match and match the content to the rule according to the requirements.

For example://Specify string regular rule string regex = "\\b[a-z]{3}\\b";  Call pattern method, match a rule, return a pattern object pattern pattern = pattern.compile (regex);  Call the Matcher method, match a string, return the match object//matched string str = "da Jia Zhu Yi le, Ming Tian Bu Fang Jia, Xie xie!";   Matcher Matcher = Pattern.matcher (str);   Call the Matcher method to complete the requirement while (Matcher.find ()) {System.out.println (Matcher.start () + "..." +matcher.end ()); System.out.println ("Sub:" +str.substring (Matcher.start (), Matcher.end ()));
System.out.println (Matcher.group ());

Regular Expressions General usage

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.