grep 正则表达式空白行为 [英] grep regex whitespace behavior

查看:24
本文介绍了grep 正则表达式空白行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,包含如下内容:

I have a textfile, containing something like:

12,34 EUR 
 5,67 EUR
 ...

'EUR' 前有一个空格,我忽略 0,XX EUR.

There is one whitespace before 'EUR' and I ignore 0,XX EUR.

我试过:

grep '[1-9][0-9]*,[0-9]{2}sEUR' =>不匹配!

grep '[1-9][0-9]*,[0-9]{2} EUR' =>成功了!

grep '[1-9][0-9]*,[0-9]{2}s*EUR' =>成功了!

grep '[1-9][0-9]*,[0-9]{2}s[E]UR' =>成功了!

谁能解释一下,为什么我不能使用 ss*s[E] 匹配?

Can somebody explain me pls, why I can't use s but s* and s[E] matched?

操作系统:Ubuntu 10.04,grep v2.5

OS: Ubuntu 10.04, grep v2.5

推荐答案

这看起来像是 grep 2.5 和较新版本之间 s 处理的行为差异(旧 grep 中的错误?).我使用 grep 2.5.4 确认了您的结果,但是在使用 grep 2.6.3 (Ubuntu 10.10) 时,您的所有四个 grep 都可以正常工作.

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).

注意:

GNU grep 2.5.4
echo "foo bar" | grep "s"
   (doesn't match)

GNU grep 2.6.3
echo "foo bar" | grep "s"
foo bar

可能更少麻烦(因为 s 没有记录):

Probably less trouble (as s is not documented):

Both GNU greps
echo "foo bar" | grep "[[:space:]]"
foo bar

我的建议是避免使用 s ... 使用 [ ]*[[:space:]] 或取而代之的是类似的东西.

My advice is to avoid using s ... use [ ]* or [[:space:]] or something like it instead.

这篇关于grep 正则表达式空白行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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