grep正则表达式空白行为 [英] grep regex whitespace behavior
问题描述
12,34 EUR
5,67 EUR $ b我有一个文本文件,包含如下内容: $ b ...
在'EUR'之前有一个空格,我忽略0,XX EUR。
我试过了:
grep'[1-9] [0-9 ] *,[0-9] \ {2 \} \sEUR'=>不匹配!
$ b
grep'[1-9] [0-9] *,[0- 9] \ {2 \} EUR'=>
$ b
grep'[1-9] [0-9] *,[0-9] \\ \\ {2 \} \s * EUR'=>
$ b
grep'[1-9] [0-9] *,[0-9] \\ \\ {2 \} \s [E] UR'=>工作!
有人可以解释我为什么我不能使用 \ s
但是 \s *
和 \s [E]
匹配?
操作系统:Ubuntu 10.04,grep v2.5
GNU grep 2.5.4
echofoo bar| grep\s
(不匹配)
而
GNU grep 2.6.3
echofoo bar| grep\s
foo bar
可能不那么麻烦(如<$ c $没有文档记录):
$ $ p $ GNU greps
echo foo bar| grep[[:space:]]
foo bar
我的建议是避免使用 \ s
...使用 [\t] *
或 [[空间:]]
或类似的东西。
I have a textfile, containing something like:
12,34 EUR
5,67 EUR
...
There is one whitespace before 'EUR' and I ignore 0,XX EUR.
I tried:
grep '[1-9][0-9]*,[0-9]\{2\}\sEUR' => didn't match !
grep '[1-9][0-9]*,[0-9]\{2\} EUR' => worked !
grep '[1-9][0-9]*,[0-9]\{2\}\s*EUR' => worked !
grep '[1-9][0-9]*,[0-9]\{2\}\s[E]UR' => worked !
Can somebody explain me pls, why I can't use \s
but \s*
and \s[E]
matched?
OS: Ubuntu 10.04, grep v2.5
This looks like a behavior difference in the handling of \s
between grep 2.5 and newer versions (a bug in old grep?). I confirm your result with grep 2.5.4, but all four of your greps do work when using grep 2.6.3 (Ubuntu 10.10).
Note:
GNU grep 2.5.4
echo "foo bar" | grep "\s"
(doesn't match)
whereas
GNU grep 2.6.3
echo "foo bar" | grep "\s"
foo bar
Probably less trouble (as \s
is not documented):
Both GNU greps
echo "foo bar" | grep "[[:space:]]"
foo bar
My advice is to avoid using \s
... use [ \t]*
or [[:space:]]
or something like it instead.
这篇关于grep正则表达式空白行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!