Regular Expression Matches

$BASH_REMATCH is an array and contains the matched text snippets. ${BASH_REMATCH[0]} contains the complete match. The remaining elements, e.g. ${BASH_REMATCH[1]}, contain the portion which were matched by () subexpressions.

Examples

Print matches :

1
2
3
4
5
text="name=value with spaces"
[[ $text =~ ^([a-z]+)=(.*)$ ]]
echo all: "${BASH_REMATCH[0]}"
echo name: "${BASH_REMATCH[1]}"
echo value: "${BASH_REMATCH[2]}"

Output:

all: name=value with spaces
name: name
value: value with spaces
BashSupport Pro 是一个 支持 $BASH_REMATCH 的 Bash 集成开发工具 – 马上试用!
© 2020–2022 Joachim Ansorg
声明
隐私政策
许可条款