grep和正则表达式 - 我为什么要逃避花括号? [英] Grep and regex - why am I escaping curly braces?

查看:597
本文介绍了grep和正则表达式 - 我为什么要逃避花括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我深感不解的grep的方式似乎解析正则表达式:

I'm deeply puzzled by the way grep seems to parse a regex:

$ echo "@NS500287" | grep '^@NS500[0-9]{3}'
#nothing
$ echo "@NS500287" | grep '^@NS500[0-9]\{3\}'
@NS500287

这不可能是正确的。为什么我逃脱大括号是一个匹配previous,N次组件的一部分(而不是,比方说,方括号内为好)?

That can't be right. Why am I escaping curly brackets that are part of a "match the previous, N times" component (and not, say, the square brackets as well)?

不应该逃避,只有当我在写,实际上相匹配的正则表达式是必要的 {} 作为在查询字符串的字符?

Shouldn't escaping be necessary only when I'm writing a regex that actually matches { and } as literal characters in the query string?

更多的道出的比什么都重要,但我很好奇的答案。

More of a cri de coeur than anything else, but I'm curious about the answer.

推荐答案

这是因为 {} 特殊字符,他们需要不同的处理有这个特殊的行为。否则,他们将被视为文字 {}

This is because {} are special characters and they need to handled differently to have this special behaviour. Otherwise, they will be treated as literal { and }.

您可以逃脱像你这样:

$ echo "@NS500287" | grep '^@NS500[0-9]\{3\}'
@NS500287

或使用的grep -E

$ echo "@NS500287" | grep -E '^@NS500[0-9]{3}'
@NS500287

未经过任何处理:

$ echo "he{llo" | grep "{"
he{llo


的grep人

-E ,--extended-正则表达式

-E, --extended-regexp

国米preT图案作为扩展的正前pression(ERE,见下文)。
  (-E由POSIX指定)。

Interpret PATTERN as an extended regular expression (ERE, see below). (-E is specified by POSIX.)

...

常规EX preSSIONS

一个普通的前pression是描述一组字符串的模式。
  普通的前pressions的构造类似于算术
  前pressions,通过使用不同的运营商合并较小
  前pressions。

A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to arithmetic expressions, by using various operators to combine smaller expressions.

grep的理解三个不同的版本经常EX pression的
  语法:基本,扩展和perl会在GNU的grep,有
  基本功能和扩展之间提供的功能没有区别
  语法。在其他实现方式中,基本的正前pressions是
  那么强大。下面描述适用于扩展的正
  前pressions;基本规律EX pressions差异汇总
  之后。 Perl的正前pressions提供附加的功能,
  并记录在pcresyntax(3)和pcrepattern(3),但可能不
  每个系统均可。

grep understands three different versions of regular expression syntax: "basic," "extended" and "perl." In GNU grep, there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards. Perl regular expressions give additional functionality, and are documented in pcresyntax(3) and pcrepattern(3), but may not be available on every system.

...

基本VS扩展正防爆pressions

在基本的正前pressions的元字符,+,{,|,(和)失去其特殊含义;而是使用的有反斜杠
  版本\\ ?, +,{\\ |,(和)

这篇关于grep和正则表达式 - 我为什么要逃避花括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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