string: Add "--groups-only" to match

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 #6056
This commit is contained in:
Fabian Homborg
2019-06-11 16:05:24 +02:00
parent 801d7e3e11
commit f3f6e4a982
3 changed files with 59 additions and 4 deletions

View File

@@ -730,3 +730,28 @@ string escape \x7F
string pad -w 8 he \eh
# CHECK: he
# CHECK: {{\x1bh}}
string match -rg '(.*)fish' catfish
# CHECK: cat
string match -rg '(.*)fish' shellfish
# CHECK: shell
# An empty match
string match -rg '(.*)fish' fish
# No match at all
string match -rg '(.*)fish' banana
# Make sure it doesn't start matching something
string match -r --groups-only '(.+)fish' fish
echo $status
# CHECK: 1
# Multiple groups
string match -r --groups-only '(.+)fish(.*)' catfishcolor
# CHECK: cat
# CHECK: color
# Examples specifically called out in #6056.
echo "foo bar baz" | string match -rg 'foo (bar) baz'
# CHECK: bar
echo "foo1x foo2x foo3x" | string match -arg 'foo(\d)x'
# CHECK: 1
# CHECK: 2
# CHECK: 3