Bash regexp.*匹配范围太远 [英] Bash regexp .* matches too far

查看:69
本文介绍了Bash regexp.*匹配范围太远的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下内容的文件input.txt:

I have a file input.txt with the following content:

foo
[assembly: AssemblyVersion("1.2.3")]
bar")]
quux

要匹配输入中的 1.2.3 ,请使用以下脚本:

To match the 1.2.3 from the input the following script is used:

#!/bin/bash
regex='\[assembly: AssemblyVersion\("(.*)"\)\]'
fileContent=$(cat input.txt)
[[ "$fileContent" =~ $regex ]]
echo "${BASH_REMATCH[1]}"

我希望输出为 1.2.3 ,但是它是:

I would expect the output to be 1.2.3 but it is:

1.2.3")]
bar

为什么会这样?如何解决?

Why is that so? How to fix it?

位于 https://regex101.com 的正则表达式测试器可以正常工作.

The regular expressions tester at https://regex101.com works as expected.

推荐答案

.* 被称为贪婪点匹配子模式,它与"),任何字符包括换行符.

The .* is called a greedy dot matching subpattern and it matches ", and ), any character including a newline.

因此,限制贪婪的最佳技巧是使用否定的字符类 [^] ,该类将匹配除" 以外的任何字符(如果不能包含引号)在带引号的字符串中):

Thus, the best trick to limit the greediness is using a negated character class [^"] that will match any character but " (if there can be no quotes inside the quoted string):

'\[assembly: AssemblyVersion\("([^"]*)"\)\]'
                                ^^^^^ 

演示

或-如果加引号的字符串内不应该包含():

or - if there should be no ( and ) inside the quoted string:

'\[assembly: AssemblyVersion\("([^()]*)"\)\]'
                                ^^^^^  

演示

这篇关于Bash regexp.*匹配范围太远的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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