Skip to content
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

regex looks like it has a bug about grouping but not capturing #215

Open
dzpao opened this issue Mar 9, 2024 · 3 comments
Open

regex looks like it has a bug about grouping but not capturing #215

dzpao opened this issue Mar 9, 2024 · 3 comments

Comments

@dzpao
Copy link
Contributor

dzpao commented Mar 9, 2024

#help regex:

     { } embed a raw regular expression, matches are stored to %1-%99.
   %!{ } embed a raw regular expression, matches are not stored.

Minimal Workable Example:

2024-03-09 19:28:56 INPUT: #if { "foobar" == "{foo|bar}" } {#echo OK} {#echo BAD}
BAD
2024-03-09 19:29:00 INPUT: #if { "foobar" == "%!{foo|bar}" } {#echo OK} {#echo BAD}
OK

Since you can't use ^ and $ in #if, there is effectively no workaround for this error.

2024-03-09 19:36:10 INPUT: #if { "foobar" == "^%*{foo|bar}%*$" } {#echo OK} {#echo BAD}
BAD
2024-03-09 19:36:14 INPUT: #if { "foobar" == "^%*%!{foo|bar}%*$" } {#echo OK} {#echo BAD}
BAD
@dzpao
Copy link
Contributor Author

dzpao commented Mar 9, 2024

I looked at the source code, and unlike {foo|bar} which would be compiled to ^(foo|bar)$, it seems that %!{foo|bar} is compiled to ^foo|bar$ instead of what I thought was ^(?:foo|bar)$, which led to me actually having to pass %!{(:?foo|bar)} in order to get to my goal, and to be honest, it looks kind of ugly.

@scandum
Copy link
Owner

scandum commented Mar 9, 2024

It's explained in the help file:

[ ] . + | ( ) ? * are treated as normal text unlessed used within
braces. Keep in mind that { } is replaced with ( ) automatically
unless %!{ } is used.

@nyankers
Copy link

nyankers commented Apr 9, 2024

You can also do {?:foo|bar} since the { }s are replaced by ( )s verbatim.

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