正则表达式 – 如何使用带有字符类的加号作为正则表达式的一部分?

前端之家收集整理的这篇文章主要介绍了正则表达式 – 如何使用带有字符类的加号作为正则表达式的一部分?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在cygwin中,这不会返回匹配:

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

但这确实会返回一个匹配:

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

这两个表达式不一样吗?
有没有办法表达“字符类的一个或多个字符”而不键入两次字符类(如在秒示例中)?

根据this link,两个表达式应该是相同的,但也许Regular-Expressions.info不包括cygwin中的bash.

解决方法

grep有多个匹配的“模式”,默认情况下只使用一个基本集,除非它们被转义,否则它们不识别多个元字符.您可以将grep放入扩展或perl模式以进行评估.

从男人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.

猜你在找的正则表达式相关文章