Java and regular expressions: Why do the two matches mean the same? -Php Tutorial

Source: Internet
Author: User
{Code...} matches hopeful. If yes, the hope in hopeful is captured. Of course, continue to search for matched substrings from f. The comparison shows that ,(? Hopeful) hope and hope (? Ful), the effects of the two regular expressions are the same, although the above descriptions are still...
javaPattern p = Pattern.compile("(?=hopeful)hope");  String str = "hopeful";  Matcher m = p.matcher(str);  while(m.find()){     System.out.println(m.group());  }

Whether it can match hopeful. if it can, the hope in hopeful is captured.

Of course, continue to search for matched substrings from f. The comparison shows that ,(? = Hopeful) hope and hope (? = Ful). The two regular expressions have the same effect.

Although I can't understand the above description, I only saw hope (? = Ful) how can I understand this writing method? I always thought (? =) Is used to match whether the form ends with 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 it can match hopeful. if it can, the hope in hopeful is captured.

Of course, continue to search for matched substrings from f. The comparison shows that ,(? = Hopeful) hope and hope (? = Ful). The two regular expressions have the same effect.

Although I can't understand the above description, I only saw hope (? = Ful) how can I understand this writing method? I always thought (? =) Is used to match whether the form ends with xxx...

(? = Exp) match the position before exp

(?=hopeful)hope-> (? = Hopeful) + hope

(? = Hopeful) if the h index in hopeful is backward matched, hope is found.

hope(?=ful)-> Hope + (? = Ful)

Find hope first and then match the ful index.hope(?=ful)fulMatchinghopeful

You can assume that the match is to move the cursor to the text editor.MatchStart position of the text, and selectCaptureText.
Hope matches| Hope... The vertical line indicates the cursor position, and the next match starts after e.
(? Yes,The capture length is 0., The effect is equivalent to... | ful
The combination of the two is to match the entireHopeYes, but only hope is captured.

(? Hopeful) the matching effect is | hopeful, which matches the entire hopeful, and the next match still starts from h.
In the future, hope matches| Hope...
The effect is the same as the regular expression above.

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.