私が本当に知る必要があるのは:
- どういう
(?(
意味ですか? - どういう
?:
意味ですか?
私が理解しようとしている正規表現は次のとおりです。
(次の正規表現の上記の記号に注意してください)
(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
(?(?=and )(and )|(blah))
パターンは if- then - (?(expression)yes|no)
elseのように使用され and
ますand
。blah
(?:)
は非キャプチャ グループです。そのため、グループに含まれたり、後方参照として使用されたりしません \1
そう、
(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
一致します
and 1900
blah2000
and 2012
blah2013
注(すべてグループに関するものです)
この正規表現で同じことを達成できます
(and |blah)(?:[1][9]|[2][0])[0-9][0-9]
。これらの正規表現の唯一の違いは、形成されるグループの数です。
したがって、私の正規表現は、またはのいずれかを含む1つのグループを形成しand
ますblah
あなたの正規表現はグループを形成しません.一致する場合にのみグループを形成しblah
ます..
いくつかのパターンのクイック リファレンスを次に示します。
. Any character except newline.
\. A period (and so on for \*, \(, \\, etc.)
^ The start of the string.
$ The end of the string.
\d,\w,\s A digit, word character [A-Za-z0-9_], or whitespace.
\D,\W,\S Anything except a digit, word character, or whitespace.
[abc] Character a, b, or c.
[a-z] a through z.
[^abc] Any character except a, b, or c.
aa|bb Either aa or bb.
? Zero or one of the preceding element.
* Zero or more of the preceding element.
+ One or more of the preceding element.
{n} Exactly n of the preceding element.
{n,} n or more of the preceding element.
{m,n} Between m and n of the preceding element.
??,*?,+?,
{n}?, etc. Same as above, but as few as possible.
(expr) Capture expr for use with \1, etc.
(?:expr) Non-capturing group.
(?=expr) Followed by expr.
(?!expr) Not followed by expr.
式(?(?=and )(and )|(blah))
はif-else
式です:)
ここで正規表現をテストできます: Regexpal.com
?:
非キャプチャ グループです。
(?ifthen|else)
if, then 式を構築するために使用されます。
詳細については、こちらをご覧ください。