how to find spaces with a regex?

Bevan C. Bennett bevan at fulcrummicro.com
Sat Feb 21 02:25:58 UTC 2004


Nix, Robert P. wrote:
> grep " *" filename will also work, as does "\s*". "\s\{2,3\}" did not work for me, but " \{2,3\}" does. Actually, to get the desired results, I ended up using a regex of "\b \{2,3\}\b", because {2,3} matched three spaces, but really didn't care if the character following it was also a space.
> 
> grep "\b[:space:]*\b" filename works, but not "\b[:space:]\{2,3\}\b".
> 
> The command "perl -n -e "print if m/\b\s{2,3}\b/" filename works as does "perl -n -e "print if m/\b {2,3}\b/" filename .
> 
> So my question becomes: In grep, why won't the braced notation work with the symbolics for spaces when * and + do?

grep "\b[[:space:]]\{2,3\}\b" filename works.
According to the manpage, [:space:] is only valid inside a bracket, so I 
don't know why "\b[:space:]*\b" works...

I don't think grep actually uses \s to mean a space... at least 'man 
grep' doesn't appear to mention it... although many other regexps do.





More information about the fedora-list mailing list