如何在字符类中使用加号作为正则表达式的一部分? [英] How do you use a plus symbol with a character class as part of a regular expression?

查看:60
本文介绍了如何在字符类中使用加号作为正则表达式的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在cygwin中,这不会返回匹配项:

in cygwin, this does not return a match:

$ echo "aaab" | grep '^[ab]+$'

但这会返回一个匹配项:

But this does return a match:

$ echo "aaab" | grep '^[ab][ab]*$'
aaab

两个表达式是否不相同?有没有什么方法可以表达一个或多个字符类的字符"而无需两次键入字符类(例如在秒示例中)?

Are the two expressions not identical? Is there any way to express "one or more characters of the character class" without typing the character class twice (like in the seconds example)?

根据此链接,两个表达式应该相同,但也许正则表达式-Expressions.info不涵盖cygwin中的bash.

According to this link the two expressions should be the same, but perhaps Regular-Expressions.info does not cover bash in cygwin.

推荐答案

grep 具有多个模式"匹配,并且默认情况下仅使用基本集,该基本集不能识别许多元字符除非他们逃脱了.您可以将grep设置为扩展或perl模式,以便对 + 进行评估.

grep has multiple "modes" of matching, and by default only uses a basic set, which does not recognize a number of metacharacters unless they're escaped. You can put grep into extended or perl modes to let + be evaluated.

来自 man grep :

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

  -P, --perl-regexp
     Interpret PATTERN as a Perl regular expression.  This is highly experimental and grep -P may warn of unimplemented features.


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

  Traditional egrep did not support the { meta-character, and some egrep implementations support \{ instead, so portable scripts should avoid { in grep -E patterns and should use [{] to match a literal {.

  GNU  grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an invalid interval specification.  For example, the command grep -E '{1' searches for the two-character string {1 instead of reporting a syntax
       error in the regular expression.  POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.

或者,您可以使用 egrep 代替 grep -E .

Alternately, you can use egrep instead of grep -E.

这篇关于如何在字符类中使用加号作为正则表达式的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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