正则表达式匹配一个或多个反斜杠和+符号

时间:2011-05-19 23:47:56

标签: regex perl

我正在编写一个正则表达式来匹配一个或多个\和一个或多个+符号。 这就是我到目前为止看起来不正确的

/\\+\++/

my $test = "\+";

if( $test =~ /\\+\++/)
{

    print "yes";

}

我已经尝试过这段代码而且无效

2 个答案:

答案 0 :(得分:2)

对我来说很好看:

perl -Dr -e '"\\+++" =~ /\\+\++/'
Compiling REx "\\+\++"
rarest char + at 1
rarest char \ at 0
Final program:
   1: PLUS (4)
   2:   EXACT <\\> (0)
   4: PLUS (7)
   5:   EXACT <+> (0)
   7: END (0)
anchored "\" at 0 floating "\+" at 0..2147483647 (checking floating) plus minlen 2
Omitting $` $& $' support.

EXECUTING...

Guessing start of match in sv for REx "\\+\++" against "\+++"
Found floating substr "\+" at offset 0...
Found anchored substr "\" at offset 0...
Guessed: match at offset 0
Matching REx "\\+\++" against "\+++"
   0 <> <\+++>               |  1:PLUS(4)
                                  EXACT <\\> can match 1 times out of 2147483647...
   1 <\> <+++>               |  4:  PLUS(7)
                                    EXACT <+> can match 3 times out of 2147483647...
   4 <\+++> <>               |  7:    END(0)
Match successful!
Freeing REx: "\\+\++"

答案 1 :(得分:0)

你的表达很好,但如果整个字符串只应由\&amp;组成,你可能想要添加锚点。 +字符。

/^\\+\++$/g

您可以在http://refiddle.com/18e

查看测试和示例