Bash脚本在正则表达式上不匹配 [英] Bash script wont match on regular expression
问题描述
我有以下bash脚本,应该生成输出TEST
I have the following bash script which should be producing the output TEST
#!/bin/bash
test="TEST:THING - OBJECT_X"
if [[ $test =~ ^([a-zA-Z0-9]+)\:([a-zA-Z0-9]+)[A-Z\s\-_]+$ ]]; then
echo ${BASH_REMATCH[1]}
fi
在我的正则表达式测试器中,正则表达式似乎在第一组和第二组上匹配并捕获:
In my regex tester the regular expression seems to be matching and capturing on the first and second groups:
https://regex101.com/r/kR1jM7/1
您知道是什么原因造成的吗?
Any idea whats causing this?
推荐答案
\s
是在ERE内部没有意义的PCRE构造.使用[:space:]
代替.另外,不要将破折号转义为\-
,而是将-
移到字符集定义的最后.
\s
is a PCRE construct not meaningful inside of ERE. Use [:space:]
instead. Also, instead of escaping the dash as \-
, move the -
to the very end of the character set definition.
以下作品:
[[ $test =~ ^([a-zA-Z0-9]+):([a-zA-Z0-9]+)[A-Z[:space:]_-]+$ ]]
也就是说,为了与更大范围的bash版本兼容,请将正则表达式移至变量中:
That said, for compatibility with a wider range of bash releases, move the regex into a variable:
re='^([a-zA-Z0-9]+):([a-zA-Z0-9]+)[A-Z[:space:]_-]+$'
[[ $test =~ $re ]]
要更积极地使用POSIX字符类(从而使您的代码更有可能在各种语言和语言环境中正常工作),请考虑:
To use POSIX character classes more aggressively (and thus make your code more likely to work correctly across languages and locales), also consider:
re='^([[:alnum:]]+):([[:alnum:]]+)[[:upper:][:space:]_-]+$'
这篇关于Bash脚本在正则表达式上不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!