7

私が本当に知る必要があるのは:

  1. どういう(?(意味ですか?
  2. どういう?:意味ですか?

私が理解しようとしている正規表現は次のとおりです。

(次の正規表現の上記の記号に注意してください)

(?(?=and )(and )|(blah))(?:[1][9]|[2][0])[0-9][0-9]
4

4 に答える 4

3

(?(?=and )(and )|(blah))パターンは if- then - (?(expression)yes|no) elseのように使用され andますandblah

(?:)は非キャプチャ グループです。そのため、グループに含まれたり、後方参照として使用されたりしません \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ます..

于 2012-10-17T11:15:37.740 に答える
2

いくつかのパターンのクイック リファレンスを次に示します。

.   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

于 2012-10-17T11:16:43.790 に答える
2
(?:...)

は非キャプチャグループです。と同じように機能しますが、後で再利用するため(...)の後方参照 (など) を作成しません。\1

(?(condition)true|else)

一致しようとする条件conditionです。成功した場合は との一致を試み、失敗したtrue場合は との一致を試みelseます。

使用例があまりないため、これはめったに見られない正規表現構造です。あなたの場合、

(?(?=and )(and )|(blah))

のように書き換えることができた

(and |blah)
于 2012-10-17T11:17:46.160 に答える
0

?:非キャプチャ グループです。 (?ifthen|else)if, then 式を構築するために使用されます。

詳細については、こちらをご覧ください。

http://www.regular-expressions.info/conditional.html

于 2012-10-17T11:16:18.097 に答える