$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 contain the matches of () subexpressions. For example, ${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 of a regular expression with $BASH_REMATCH
1
2
3
4
5
text="name=value with spaces"
[[ $text =~ ^([a-z]+)=(.*)$ ]]
echo complete match: "${BASH_REMATCH[0]}"
echo name: "${BASH_REMATCH[1]}"
echo value: "${BASH_REMATCH[2]}"
Output:
1
2
3
complete match: name=value with spaces
name: name
value: value with spaces
BashSupport Pro 是一个 支持 $BASH_REMATCH 的 Bash 集成开发工具 – 马上试用
© 2020–2024 Joachim Ansorg
声明
隐私政策
许可条款