Grep 和正则表达式 - 为什么我要转义大括号? [英] Grep and regex - why am I escaping curly braces?

查看:62
本文介绍了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

这不可能是对的.为什么我要转义作为匹配前 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?

cri de coeur 比什么都重要,但我对答案很好奇.

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

你可以像以前一样逃脱:

You can either escape like you did:

$ 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

<小时>

来自man grep:

-E, --extended-regexp

-E, --extended-regexp

将 PATTERN 解释为扩展的正则表达式(ERE,见下文).(-E 由 POSIX 指定.)

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

...

正则表达式

正则表达式是描述一组字符串的模式.正则表达式的构造类似于算术表达式,通过使用各种运算符来组合较小的表达.

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 理解三种不同版本的正则表达式语法:基本"、扩展"和perl".在 GNU grep 中,有基本和扩展之间的可用功能没有区别语法.在其他实现中,基本的正则表达式是不那么强大.以下描述适用于扩展的常规表达式;总结了基本正则表达式的差异然后.Perl 正则表达式提供了额外的功能,并且记录在 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.

...

基本与扩展正则表达式

在基本的正则表达式中,元字符 ?, +, {, |, (, and ) 失去了它们的特殊意义;而是使用反斜线版本 ?, +, {, |, ().

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions ?, +, {, |, (, and ).

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

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