Skip to content

Continuation of lookahead true vs. false case. #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
swgmike opened this issue Dec 28, 2022 · 3 comments
Closed

Continuation of lookahead true vs. false case. #179

swgmike opened this issue Dec 28, 2022 · 3 comments

Comments

@swgmike
Copy link

swgmike commented Dec 28, 2022

Based on premature closure of #178 before I could respond.

Please explain why it highlights both for the following at regex101 to match both if and mike when one should only be highlighted based on the help at regex101 to match true or false. It should match true only which is if . Regex101 says there's two matches.
If the positive lookahead is to match both, then the help files everywhere on the internet need to be corrected.

(?<=[^:])(if)|(mike)

string: Is candy if or mike?

@PhilipHazel
Copy link
Collaborator

You didn't need to open a new issue: you can comment on closed issues.....

I cannot comment on the workings of regex101, only on the way PCRE2 actually matches. The pattern you quote above matches EITHER "if" when not preceded by a colon OR "mike". If regex101 says there are two matches, maybe it is doing a global match?
Here is a pcre2test that does just that (note the /g):

PCRE2 version 10.42 2022-12-11
/(?<=[^:])(if)|(mike)/g
Is candy if or mike
0: if
1: if
0: mike
1:
2: mike

@swgmike
Copy link
Author

swgmike commented Dec 28, 2022

The comment button wasn't activated after trying to enter a comment on the last issue #178.

Your listing above is what regex101 shows as two matches when only expecting one.

However, below is an example from their website for the postiive lookahead which works as expected should one change the pattern from "is" to "isa" which highlight the true vs. false even the the groups are OR'd.

The regex101 help on positive lookahead

(?(?=...)yes|no)
If the lookahead succeeds, matches the pattern before the vertical bar. Otherwise, matches the pattern after the vertical bar. The lookaround can be negative also. Global flag breaks conditionals.

True casae highlights "is delicious"
/(?(?=is)(is delicious)|(disgusting))/
Candy is delicious or disgusting.

Final outcome: I will close this problem since I have the global flag set which causes the multiple match. If turned off it will only match one or the other. If multiline turned on only, then it will match one or the other too. So I was mistaken. The help info also stated "Global flag breaks conditionals." which I didn't catch.
Thanks for the help.

@swgmike swgmike closed this as completed Dec 28, 2022
@carenas
Copy link
Contributor

carenas commented Dec 30, 2022

The comment button wasn't activated after trying to enter a comment on the last issue #178.

Not sure if that might change in the future, but usually the comment button activates itself once anything is written.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants