Java, Regular: Why do the two matches mean the same thing?

Source: Internet
Author: User
javaPattern p = Pattern.compile("(?=hopeful)hope");  String str = "hopeful";  Matcher m = p.matcher(str);  while(m.find()){     System.out.println(m.group());  }

Whether the hopeful can be matched and, if so, captures the hope in hopeful.

Of course continue to look backwards for matching substrings, starting with F. The comparison can be seen, (=hopeful) Hope and Hope (? =ful), two regular effect is actually the same

Although see the above explanation still can't understand, saw Hope (? =ful) This writing, another kind of writing how to understand? I always thought (? =) is used to match whether or not to end this form of xxx ...

Reply content:

javaPattern p = Pattern.compile("(?=hopeful)hope");  String str = "hopeful";  Matcher m = p.matcher(str);  while(m.find()){     System.out.println(m.group());  }

Whether the hopeful can be matched and, if so, captures the hope in hopeful.

Of course continue to look backwards for matching substrings, starting with F. The comparison can be seen, (=hopeful) Hope and Hope (? =ful), two regular effect is actually the same

Although see the above explanation still can't understand, saw Hope (? =ful) This writing, another kind of writing how to understand? I always thought (? =) is used to match whether or not to end this form of xxx ...

(? =exp) matches the position of the exp front

(?=hopeful)hope(? =hopeful) + Hope

(? =hopeful) positioning is the index of H in hopeful to match, then found the hope

hope(?=ful)Hope + (? =ful)

First find hope after the match ful index can try hope(?=ful)ful to matchhopeful

You can assume that the match is to move the cursor to the beginning of the matched text in the text editor and check the capture text.
Hope matching effect is |hope... The vertical bar represents the cursor position, and the next match starts after E.
(? ful) matches the ful, but the capture length is 0, the effect is equal to ... | Ful
The combination of both matches the entire hopeful but captures only hope

The (? hopeful) match effect is |hopeful, matching the entire hopeful, and the next match still starts with H
Subsequent hope matching effect is |hope...
The effect is the same as the above regular.

  • 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.