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
regexp: *? not always preferring fewer #22940
Comments
Seems to be related to However, that matches the first |
To give a bit of context, I was trying to use the regexp package to implement Bash pattern matching, in particular for prefix/suffix stripping:
|
FindString is documented to return the leftmost match. |
You're absolutely right, I missed that entirely. I'm still wondering if this is at all possible, but at least it's not a bug in the regexp package. |
This is a little hacky, but I think you can get what you want by reversing the regexp and the string. |
Thanks for the tip - something similar had occurred to me, but I was thinking there had to be a simpler way. Perhaps using a leading |
Also, I realise that the |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What did you do?
https://play.golang.org/p/vUEyH8riLq
What did you expect to see?
The shortest match:
What did you see instead?
The longest match:
I am also slightly confused as to how the greedy
*
versus the non-greedy*?
interact with.Longest
. One way or another, if I use the non-greedy repetitions, and I don't call.Longest
, I would assume I would get the shortest match.Unless this is a restriction coming from how the regexp package is implemented internally. I hope that isn't the case, as I don't see a way to get the shortest match otherwise. Or, if I'm doing something wrong and there's a way to do it, perhaps the godoc could be clearer.
/cc @rsc @matloob as per golang.org/s/owners
The text was updated successfully, but these errors were encountered: