-
Notifications
You must be signed in to change notification settings - Fork 1.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
String match with capturing group #6056
Comments
This looks like expected behavior to me. You can add a filter to extract the capture group: string match -r "_([^_]*)\." "very_nice_file_whatmatter.csv" | awk 'NR == 2' (or |
And was the behavior changed? Because the code in the linked issue (comment) yielding different result for me:
I know I can refine the output, but it looks like a regression or a bug, if it changed since that comment. |
No, that was just an oversight. It was always "/weird/". If you want to have just a certain part of the input, use Also I'm preparing an additional "--groups-only" option to |
The full match is considered a group, every single regex implementation that I know of defines the entire match to be capture group 0, and further explicit capturing groups are numbered from there. |
I just ran into this with 912421f, which could have been much shorter if there were some way to change the default behavior. I don't think echo "foo bar baz" | string match -r 'foo (bar) baz' '$2' would only print the second group (the first explicitly captured group). To do this now requires using # Try to extract the numbers found in each 'foo_x' occurence
# this prints the entire word each time, followed by the number
echo "foo1x foo2x foo3x" | string match -ar 'foo(\d)x'
# this prints the number only, but does not match more than once
echo "foo1x foo2x foo3x" | string replace -r '.*foo(\d)x.*' '$1'
# prints only the very last match `3` You can hack around it by using My suggestion is going with something like echo "foo1x foo2x foo3x | string match -ar 'foo(\d)x' '$1' or echo "foo1x foo2x foo3x | string match -ar 'foo(\d)x' --print '$1' # or similar to tell |
This isn't possible. What should string match -r 'foo (bar) baz' '$2' "foo bar baz" do? Anything after the first parameter is currently a "string" to act on, this can't be optional or it's ambiguous. Anyway, I fail to see how --groups-only wouldn't be enough, this would just be echo "foo bar baz" | string match -r --groups-only 'foo (bar) baz'
Depending on what you want, echo "foo1x foo2x foo3x" | string replace -ar 'foo(\d)x' '$1' would work, which yields one string "1 2 3".
is echo "foo1x foo2x foo3x" | string match -ar --groups-only 'foo(\d)x' which would yield "1", "2" and "3". Anyway, I gotta get that branch cleaned up and merged. |
Alright, I just added This manages to solve all examples people came up with here, as far as I see, so I don't see the need for further transformations. Of course we can still add that later. |
This adds a simple way of picking bits from a string that might be a bit nicer than having to resort to a full `replace`. Fixes fish-shell#6056
fish seems to broke the behavior of
string match
.I'm trying to match the part of filename after
_
and before.
:I found this issue #4925, but the behavior there is different than what I got.
Not sure if it's the bug or a feature I'm missing.
fish, version 3.0.2
The text was updated successfully, but these errors were encountered: