Regex reading Notes (V) Java Operations Chapter

Source: Internet
Author: User

First, a demo program

Java's regular expression package is Java.util.regex, mainly using the pattern and matcher.

The GroupCount method is callable, and most methods must be called after the match attempt succeeds.

The main methods are:

Boolean find ()

The Boolean value returned indicates whether a match can be found, and if there are multiple calls, try a new match each time after the last matching position.

public class Regex {public    static void Main (string[] args) {        String regex = "\\w+";        Pattern pattern = pattern.compile (regex);        String str = "This is my";        Matcher Matcher = Pattern.matcher (str);        while (Matcher.find ()) {            String matchedtext = Matcher.group ();            int matchedfrom = Matcher.start ();            int matchedto = Matcher.end ();            System.out.println (matchedfrom+ "" +matchedto);            System.out.println (Matchedtext);        }

Boolean Find (int offset)

If an integer parameter is specified, the match attempt starts at the position of the offset character at the beginning of the target string, and if an error is exceeded, this form of find is not affected by the current search scope.

Boolean matches ()

Returns whether the regular expression can exactly match the text of the range retrieved in the target string.

Boolean Lookingat ()

Returns whether the regular expression can find a match in the current retrieval scope of the current target string.

Group ()

Returns the matching text of the previous applied regular expression.

GroupCount ()

Returns the number of captured brackets

Group (int num)

Returns the corresponding capturing brace match, group (0) equals group ()

The outputs are:

This is
2
This
Is

int start (int num)

Returns the absolute offset value in the target string of the starting point of the matched text of the captured bracket numbered Num.

int start ()

Returns the absolute low value of the entire match starting point, which is equivalent to start (0)

int end with int end (int num) corresponds to start

Regex reading Notes (V) Java Operations Chapter

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.