$BASH_REMATCH  –  Regular Expression Matches

$BASH_REMATCH is an array and contains the matches of a regular expression.

${BASH_REMATCH[0]} contains the complete match of the regular expression. The remaining elements, e.g. ${BASH_REMATCH[1]}, contain the matches of () subexpressions. ${BASH_REMATCH[1]} contains the match of the 1st () expression, ${BASH_REMATCH[2]} contains the match of the 2nd () expression, and so on.

With Bash, the conditional test operator =~ of [[ ]] evaluates a regular expression.

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]}"
BashSupport Pro 是一个 支持 $BASH_REMATCH 的 Bash 集成开发工具 – 马上试用
© 2020–2024 Joachim Ansorg
声明
隐私政策
许可条款