Grep 和正则表达式

编程入门 行业动态 更新时间:2024-10-25 21:26:20
本文介绍了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 和正则表达式

本文发布于:2023-11-03 12:25:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555129.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正则表达式   Grep

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!