为什么我的正则表达式模式不懒惰?它应该捕获第一个数字,而不是第二个。
这是一个有效的 bash 脚本..
#!/bin/bash
text='here is some example text I want to match word1 and this number 3.01 GiB here is some extra text and another number 1.89 GiB'
regex='(word1|word2).*?number[[:blank:]]([0-9.]+) GiB'
if [[ "$text" =~ $regex ]]; then
echo 'FULL MATCH: '"${BASH_REMATCH[0]}"
echo 'NUMBER CAPTURE: '"${BASH_REMATCH[2]}"
fi
这是输出...
FULL MATCH: word1 and this number 3.01 GiB here is some extra text and another number 1.89 GiB
NUMBER CAPTURE: 1.89
使用这个在线 POSIX 正则表达式测试器,正如我所料,它很懒惰。但是在 Bash 中它是贪婪的。数字捕获应该是 3.01,而不是 1.89。