关于这四个正则表达式之间的差异,略有不同

时间:2012-02-07 21:59:17

标签: regex perl

在学习正则表达式时,我曾经看过以下四个例子。我如何理解他们的不同之处?

/ABC (?i:s) XYZ/
/ABC (?x: [A-Z] \.?  \s )?XYZ/
/ABC (?ix: [A-Z] \.?  \s )?XYZ/
/ABC (?x-i: [A-Z] \.?  \s )?XYZ/i

ix标志是什么意思?

2 个答案:

答案 0 :(得分:2)

这些非常简单。快速查看documentation即可回答您的问题。您可能还会发现YAPE::Regex::Explain有用。

$ perl -MYAPE::Regex::Explain -e'
   print YAPE::Regex::Explain->new($_)->explain
      for 
         qr/ABC (?i:s) XYZ/,
         qr/ABC (?x: [A-Z] \.?  \s )?XYZ/,
         qr/ABC (?ix: [A-Z] \.?  \s )?XYZ/,
         qr/ABC (?x-i: [A-Z] \.?  \s )?XYZ/i;
'

The regular expression:

(?-imsx:ABC (?i:s) XYZ)

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):
----------------------------------------------------------------------
  ABC                      'ABC '
----------------------------------------------------------------------
  (?i:                     group, but do not capture (case-
                           insensitive) (with ^ and $ matching
                           normally) (with . not matching \n)
                           (matching whitespace and # normally):
----------------------------------------------------------------------
    s                        's'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
   XYZ                     ' XYZ'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

The regular expression:

(?-imsx:ABC (?x: [A-Z] \.?  \s )?XYZ)

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):
----------------------------------------------------------------------
  ABC                      'ABC '
----------------------------------------------------------------------
  (?x:                     group, but do not capture (disregarding
                           whitespace and comments) (case-sensitive)
                           (with ^ and $ matching normally) (with .
                           not matching \n) (optional (matching the
                           most amount possible)):
----------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
----------------------------------------------------------------------
    \.?                      '.' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  XYZ                      'XYZ'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

The regular expression:

(?-imsx:ABC (?ix: [A-Z] \.?  \s )?XYZ)

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):
----------------------------------------------------------------------
  ABC                      'ABC '
----------------------------------------------------------------------
  (?ix:                    group, but do not capture (case-
                           insensitive) (disregarding whitespace and
                           comments) (with ^ and $ matching normally)
                           (with . not matching \n) (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
----------------------------------------------------------------------
    \.?                      '.' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  XYZ                      'XYZ'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

The regular expression:

(?i-msx:ABC (?x-i: [A-Z] \.?  \s )?XYZ)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?i-msx:                 group, but do not capture (case-insensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ABC                      'ABC '
----------------------------------------------------------------------
  (?x-i:                   group, but do not capture (disregarding
                           whitespace and comments) (case-sensitive)
                           (with ^ and $ matching normally) (with .
                           not matching \n) (optional (matching the
                           most amount possible)):
----------------------------------------------------------------------
    [A-Z]                    any character of: 'A' to 'Z'
----------------------------------------------------------------------
    \.?                      '.' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  XYZ                      'XYZ'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

答案 1 :(得分:-1)

/ expr / 标记标记应用于 expr

(? 标记 : subexpr )标记应用于 subexpr

i设置忽略大小写,x设置忽略正则表达式主体中的空格。

更详细的信息可在www.regular-expressions.info上找到。