正则表达式在SunOS上使用{M,N}时未能匹配 [英] regex failed to match using {M,N} on SunOS
问题描述
我有以下示例文件和正则表达式。
testing.txt
test aa
a bc de
e aa
ba Z
testing bb
testing ac
使用egrep的正则表达式
egrep'[] {2 ,}'testing.txt
上面的正则表达式尝试在一行中查找连续的空格。
然而,返回的结果是空的。
下面的调整表达式适用于一个或多个空格。然而,这不是我想要的。
egrep'[] +'testing.txt
不起作用 - 您最好使用Perl或GNU grep来实现您想要的功能。
$ c $如果你的系统是旧的,
传统的egrep不支持
元字符
,一些egrep实现支持\ {
相反,如此可移植的脚本应避免在egrep模式中使用{
,并应使用[{]
来匹配文字<$ c这意味着,如果grep'[$ c $>
] \ {2,\}'testing.txt
另外,
egrep'[] [] +'testing.txt
似乎只是在当前情况下的解决方法,并不会扩展,但它肯定会他请暂时与你联系。I have the following sample file and regular expression.
testing.txt
testing aa a bc de e aa ba Z testing bb testing ac
my regular expression using egrep
egrep '[ ]{2,}' testing.txt
The above regular expression attempts to find contiguous white spaces in a line. However, the result returned is empty.
the regulartion expression below works for 1 or more spaces. However that is not what I want.
egrep '[ ]+' testing.txt
解决方案If your system is old, this help reference might be describing the issue:
Traditional egrep did not support the
{
metacharacter, and some egrep implementations support\{
instead, so portable scripts should avoid{
in egrep patterns and should use[{]
to match a literal{
.That means, that - if
grep '[ ]\{2,\}' testing.txt
does not work - you are better off using Perl or GNU grep to achieve what you want.Also,
egrep '[ ][ ]+' testing.txt
seems to be a workaround only in the current situation, and will not scale, but it certainly will help you for the time being.这篇关于正则表达式在SunOS上使用{M,N}时未能匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!