gpt4 book ai didi

regex - Grep模式匹配用双引号括起来的小写字符串

转载 作者:行者123 更新时间:2023-12-04 03:59:27 25 4
gpt4 key购买 nike

我在grep上遇到了一个我似乎无法弄清的问题。我正在尝试在一组源文件中搜索用双引号(C字符串)括起来的所有小写单词的实例。使用bash和gnu grep:

grep -e '"[a-z]+"' *.cpp

没有给我比赛,而
grep -e '"[a-z]*"' *.cpp

给我像“Abc”之类的匹配项,而不仅仅是小写字符。仅匹配“abc”的正则表达式是什么?

最佳答案

您忘记了转义元字符。

grep -e '"[a-z]\+"'

对于第二部分,它匹配多大小写字符的原因是由于您的语言环境。如下:
$ echo '"Abc"' | grep -e '"[a-z]\+"'
"Abc"
$ export LC_ALL=C
$ echo '"Abc"' | grep -e '"[a-z]\+"'
$

要获得“类似ascii”的行为,您需要按照grep手册页中的指定将语言环境设置为“C”:

Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale's collating sequence and character set. For example, in the default C locale, [a-d] is equivalent to [abcd]. Many locales sort characters in dictionary order, and in these locales [a-d] is typically not equivalent to [abcd]; it might be equivalent to [aBbCcDd], for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value C.

关于regex - Grep模式匹配用双引号括起来的小写字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10540305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com