Bash是否支持非贪婪的正则表达式? [英] Does Bash support non-greedy regular expressions?

查看:97
本文介绍了Bash是否支持非贪婪的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的regex模式不偷懒?它应该捕获第一个数字,而不是第二个.

How come my regex pattern isn't lazy? It should be capturing the first number, not the second.

这是一个有效的bash脚本.

Here is a working bash script..

#!/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中却很贪婪.NUMBER CAPTURE应该是3.01,而不是1.89.

Using this online POSIX regex tester it is lazy as I expected. But in Bash it is greedy. The NUMBER CAPTURE should be 3.01, not 1.89.

推荐答案

Wrt .*?

多个相邻的重复符号("+","*",?"和间隔)的行为会产生不确定的结果.

The behavior of multiple adjacent duplication symbols ( '+', '*', '?', and intervals) produces undefined results.

关于贪婪匹配,它说:

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

If the pattern permits a variable number of matching characters and thus there is more than one such sequence starting at that point, the longest such sequence is matched.

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

In this particular case you can use [^&]* instead.

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

这篇关于Bash是否支持非贪婪的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆