Grep模式匹配用双引号括起来的小写字符串 [英] Grep pattern matching lower case string enclosed in double quotes

查看:507
本文介绍了Grep模式匹配用双引号括起来的小写字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个grep问题,我似乎无法弄清楚。我试图在一组源文件中搜索括在双引号(C字符串)中的小写单词的所有实例。使用bash和gnu grep:
$ b $

  grep -e'[az] +'* .cpp 
code>

给我没有匹配,而

  grep -e'[az] *'* .cpp 

Abc不只是小写字符。什么是适当的正则表达式来匹配只有abc?

解决方案

你忘记逃离元字符。 p>

  grep -e'[az] \ +'

对于第二部分,匹配多个大小写字符的原因是因为您的语言环境。如下:

  $ echo'Abc'| grep -e'[a-z] \ +'
abc
$ export LC_ALL = C
$ echo'Abc'| grep -e'[az] \ +'
$

ascii-like行为,您需要将语言环境设置为C,如grep手册页中所述:括号表达式中,范围表达式由两个由连字符分隔的
字符组成。它匹配使用语言环境的整理顺序和字符集在两个字符之间进行排序的任何单个字符
,包括两个字符。例如,在
的默认C语言环境中,[a-d]等同于[abcd]。许多语言环境按字典顺序对字符进行排序,在这些语言环境中[a-d]是
,通常不等同于[abcd];例如,它可能等同于[aBbCcDd]。要获得
括号表达式的传统解释,可以通过将LC_ALL环境变量设置为值C来使用C语言环境。



I'm having a bit of an issue with grep that I can't seem to figure out. I'm trying to search for all instances of lower case words enclosed in double quotes (C strings) in a set of source files. Using bash and gnu grep:

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

gives me no matches, while

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

gives me matches like "Abc" which is not just lower case characters. What is the proper regular expression to match only "abc"?

解决方案

You're forgetting to escape the meta characters.

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

For the second part, the reason it is matching multi-case characters is because of your locale. As follows:

$ echo '"Abc"' | grep -e '"[a-z]\+"'
"Abc"
$ export LC_ALL=C
$ echo '"Abc"' | grep -e '"[a-z]\+"'
$

To get the "ascii-like" behavior, you need to set your locale to "C", as specified in the grep man page:

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.

这篇关于Grep模式匹配用双引号括起来的小写字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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