2

中括弧のペアの 1 つの出現と正確に一致する正規表現が必要です。

myString_{1-10}         -> match
myString_{hello}_xyz    -> match
myString_{1-10}_{hello} -> do not match

これが私の正規表現です:

(\{)[^}]*(\})

問題は、私の正規表現が、中かっこのペアが複数回出現する文字列にも一致することです...何が欠けていますか?

4

3 に答える 3

3

これを使用できます:

^[^{}]*\{[^}]*\}[^{}]*$

説明:

^[^{}]*    // Match 0 or more occurrences of character other than [{}]
 \{        // Match a `{`
 [^}]*     // Match 0 or more occurrences of character other than }
 \}        // Match a `}`
 [^{}]*$   // Match 0 or more occurrences of character other than [{}]

ネストされたブレースまたは一致しないブレースにも注意する必要があります

于 2013-07-28T21:38:00.033 に答える