-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Possible bug in regexp.QuoteMeta() #40378
Comments
This is working as expected. |
https://play.golang.org/p/qR1Dz0M5l7B Here's a version with the braces included in the regexp.QuoteMeta(). Passing the complete string for conversion to regex by MustCompile, with braces included, just escapes the braces and yields a regex that looks for the whole group of symbols together, rather than any of the symbols in the set. Escaping the braces is to be expected, though, since the example for QuoteMeta in the documentation demonstrates exactly that. |
https://play.golang.org/p/VAHU5YbWyTv Okay, final go at this for right now. This has a bunch of cases, and shows the output from The only reason that one case works here, at all, is that if the "-" is at the end of the string to escape and then compile, it gets treated as escaped (even though it's not, I guess?) because there's nothing after it to suggest that it should be treated as a regex range metachar... You can further demonstrate this by putting another character class in the string to escape, like Panic version No panic, no minus No panic, with minus So something is wrong, unless there is a reason for QuoteMeta not to escape "-" despite use as a metachar... |
Minus signs do not have to be quoted, because they are not regexp meta-characters. The fact that they are special inside of What |
Oh. Huh. I didn't realize that |
What version of Go are you using (
go version
)?According to https://www.hackerrank.com/environment
they are using 1.13... it also appears to happen on play.golang.org
Does this issue reproduce with the latest release?
Don't know, don't have time to check right now, came across this while refreshing for job interviews...
What operating system and processor architecture are you using (
go env
)?Don't know. Whatever hackrank uses, and whatever the playground uses. Not given on environment info page.
go env
OutputWhat did you do?
Writeup and code are here:
https://play.golang.org/p/t6jbBNFx44m
Summary version is that with a string of special chars as input to regexp.QuoteMeta() and passed to regexp.MustCompile(),
one ordering of the characters in the string produce an accurate count of occurrences of a minus sign/dash, and a different ordering returns an incorrect count.
Please see writeup in comments at the playground link; I found this in passing and don't have time to investigate further.
The text was updated successfully, but these errors were encountered: