Относительно различий между этими четырьмя регулярными выражениями с небольшими различиями - PullRequest
0 голосов
/ 08 февраля 2012

При изучении регулярных выражений я однажды увидел следующие четыре примера.Как я могу понять их различия?

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

Что означают флаги i и x?

Ответы [ 2 ]

2 голосов
/ 08 февраля 2012

Это очень просто. Быстрый просмотр документации ответит на ваши вопросы. Вы также можете найти 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;
'

& # x20;

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
----------------------------------------------------------------------

& # x20;

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
----------------------------------------------------------------------

& # x20;

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
----------------------------------------------------------------------

& # x20;

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
----------------------------------------------------------------------
0 голосов
/ 08 февраля 2012

/ expr / flags применяется flags to expr .

(? flags : subexpr ) применяется flags to subexpr .

i устанавливает игнорирование регистра, x устанавливает игнорирование пробелов в теле регулярного выражения.

Более подробная информация доступна на www.regular-expressions.info .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...