學習正則表達式時,我曾經看到以下四個例子。我如何理解他們的差異?關於這四個正則表達式之間的細微差別
/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
標誌是什麼意思?
學習正則表達式時,我曾經看到以下四個例子。我如何理解他們的差異?關於這四個正則表達式之間的細微差別
/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
標誌是什麼意思?
這些都很簡單。快速瀏覽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
----------------------------------------------------------------------
/
EXPR/
標誌適用標誌到EXPR。
(?
標誌:
的Subexpr )
適用標誌到的Subexpr。
i
設置爲忽略大小寫,x
設置爲忽略正則表達式主體中的空白。
更多詳細信息請登錄www.regular-expressions.info。
噢,'*(?:flags)*中的'-i'會打開區分大小寫的匹配項(禁用'/ i')。 – tripleee 2012-02-07 22:11:44
@yarek,我們試圖在SO這裏保持一定的文明程度,更不用說樂於助人。 Terse,極簡主義的回答是不鼓勵的,並且像RTFM和LMGTFY這樣公然無禮的言論是被禁止的。 – 2012-02-08 00:07:18
哪部分你不明白? – ikegami 2012-02-07 22:06:27