regex failed to match using {M,N} on SunOS

2020-04-30 02:07发布

问题:

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

回答1:

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.



标签: regex grep sunos