Perl正则表达式

时间:2012-09-21 01:54:27

标签: regex perl

我正在阅读一些涉及正则表达式并遇到麻烦的代码。

有人可以解释一下并提供一个可以解析的文本示例吗?

if(/\|\s*STUFF(\d+)\s*\|\s*STUFF(\d+)/) 
{
        $a = $1;
        $b = $2;
}

1 个答案:

答案 0 :(得分:2)

匹配的一个字符串是|STUFF1|STUFF2

YAPE::Regex::Explain

(?-imsx:\|\s*STUFF(\d+)\s*\|\s*STUFF(\d+))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  \|                       '|'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  STUFF                    'STUFF'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  \|                       '|'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  STUFF                    'STUFF'
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------