中括弧のペアの 1 つの出現と正確に一致する正規表現が必要です。
myString_{1-10} -> match
myString_{hello}_xyz -> match
myString_{1-10}_{hello} -> do not match
これが私の正規表現です:
(\{)[^}]*(\})
問題は、私の正規表現が、中かっこのペアが複数回出現する文字列にも一致することです...何が欠けていますか?
中括弧のペアの 1 つの出現と正確に一致する正規表現が必要です。
myString_{1-10} -> match
myString_{hello}_xyz -> match
myString_{1-10}_{hello} -> do not match
これが私の正規表現です:
(\{)[^}]*(\})
問題は、私の正規表現が、中かっこのペアが複数回出現する文字列にも一致することです...何が欠けていますか?
これを使用できます:
^[^{}]*\{[^}]*\}[^{}]*$
説明:
^[^{}]* // 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 [{}]
ネストされたブレースまたは一致しないブレースにも注意する必要があります