2020-11-07 02:32:22 +00:00
|
|
|
#RUN: %fish %s
|
|
|
|
# Tests for importing named regex groups as fish variables
|
|
|
|
|
|
|
|
# Capture first match
|
2020-11-27 00:23:18 +00:00
|
|
|
echo "hello world" | string match --regex -q -- '(?<words>[^ ]+) ?'
|
2020-11-07 02:32:22 +00:00
|
|
|
printf "%s\n" $words
|
|
|
|
# CHECK: hello
|
|
|
|
|
|
|
|
# Capture multiple matches
|
2020-11-27 00:23:18 +00:00
|
|
|
echo "hello world" | string match --regex -q --all -- '(?<words>[^ ]+) ?'
|
2020-11-07 02:32:22 +00:00
|
|
|
printf "%s\n" $words
|
|
|
|
# CHECK: hello
|
|
|
|
# CHECK: world
|
|
|
|
|
|
|
|
# Capture multiple variables
|
2020-12-04 17:45:08 +00:00
|
|
|
echo "hello world"\n"snello snorld" | string match -rq -- '^(?<word1>[^ ]+) (?<word2>.*)$'
|
2020-11-07 02:32:22 +00:00
|
|
|
printf "%s\n" $word1 $word2
|
|
|
|
# CHECK: hello
|
|
|
|
# CHECK: world
|
|
|
|
|
|
|
|
# Clear variables on no match
|
|
|
|
set foo foo
|
2020-11-27 00:23:18 +00:00
|
|
|
echo "foo" | string match -rq -- '^(?<foo>bar)$'
|
2020-11-07 02:32:22 +00:00
|
|
|
echo $foo
|
|
|
|
# CHECK:
|
|
|
|
|
|
|
|
# Named group may be empty in some of the matches
|
|
|
|
set word
|
|
|
|
set punctuation
|
2020-11-27 00:23:18 +00:00
|
|
|
echo "hello world, boy!" | string match -a -qr -- '(?<word>[^ .,!;]+)(?<punctuation>[.,!;])?'
|
2020-11-07 02:32:22 +00:00
|
|
|
echo $word
|
|
|
|
# CHECK: hello world boy
|
|
|
|
printf "%s\n" $punctuation
|
|
|
|
# CHECK:
|
|
|
|
# CHECK: ,
|
|
|
|
# CHECK: !
|
|
|
|
|
2020-12-04 17:45:08 +00:00
|
|
|
# Same thing with multiple arguments
|
|
|
|
set word
|
|
|
|
set punctuation
|
|
|
|
printf '%s\n' "hello world, boy!" "shello shorld, shoy!" | string match -a -qr -- '(?<word>[^ .,!;]+)(?<punctuation>[.,!;])?'
|
|
|
|
echo $word
|
|
|
|
# CHECK: hello world boy
|
|
|
|
printf "%s\n" $punctuation
|
|
|
|
# CHECK:
|
|
|
|
# CHECK: ,
|
|
|
|
# CHECK: !
|
|
|
|
|
2020-11-07 02:32:22 +00:00
|
|
|
# Verify read-only variables may not be imported
|
|
|
|
echo hello | string match -rq "(?<version>.*)"
|
|
|
|
# CHECKERR: Modification of read-only variable "version" is not allowed
|
2020-12-04 17:45:08 +00:00
|
|
|
|
|
|
|
# Verify that the *first matching argument* is used.
|
|
|
|
string match -rq '(?<bee>b.*)' -- aaa ba ccc be
|
|
|
|
echo $bee
|
|
|
|
# CHECK: ba
|