问题
This is my regex
^(settings|issues(?=a))/$
I want to match settings/ and issuesa/. The problem is I am only able to match settings/ and not issuesa/.
回答1:
The “a” in the lookahead is not part of the match.
It would match if the regex was
^(settings|issues(?=a))a/$
(though it would not match settings/ then)
or
^(settings|issues(?=a)a)/$
(but then I don't see a point for the lookahead).
回答2:
Try it
regex = '^(settings|issues(a*))/$'
You can also look it up here how regex works: https://regex101.com/r/vQr5cP/1
回答3:
With (?=a), you require a to appear at the same location in the string as /. Since / can't be a, issues(?=a) will always fail to match.
You need to put all the right-hand boundaries into lookahead constructs:
^(settings(?=/$)|issues(?=a/$))
See the regex demo
Details
^- start of string(settings(?=/$)|issues(?=a/$))- Capturing group 1:settings(?=/$)-settingssubstring that is immediately followed with/and end of string position|- orissues(?=a/$)-issuessubstring that is immediately followed witha/and end of string position.
来源:https://stackoverflow.com/questions/59299644/why-the-positive-lookahead-not-working-for-this-regex