0

为什么我的正则表达式模式不懒惰?它应该捕获第一个数字,而不是第二个。

这是一个有效的 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。

4

1 回答 1

3

Wrt .*?,POSIX标准说

多个相邻重复符号(“+”、“*”、“?”和间隔)的行为会产生未定义的结果。

关于贪心匹配,它说:

如果模式允许可变数量的匹配字符,因此从该点开始有多个这样的序列,则匹配最长的这样的序列。

在这种特殊情况下,您可以[^&]*改用。

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
NUMBER CAPTURE:  3.01
于 2019-08-23T05:43:45.073 回答