どのように私は、.NETの正規表現を使って文字列からすべての{}のトークンを抽出していますか?
質問
私は与えられた文字列から中括弧でマークされているトークンを抽出する必要があります。
私は...解析する何かを構築するためのExpressoを使って試してみました。
-------------------------------------------------------------
"{Token1}asdasasd{Token2}asd asdacscadase dfb db {Token3}"
-------------------------------------------------------------
及び製造 "TOKEN1"、 "Token2"、 "Token3"
私が使用してみました..
-------------------------------------------------------------
({.+})
-------------------------------------------------------------
...しかし、それは全体の表現に一致するように見えた。
任意の考え?
解決
試してみてください。
\{(.*?)\}
The \{ will escape the "{" (which has meaning in a RegEx). The \} likewise escapes the closing } backet. The .*? will take minimal data, instead of just .* which is "greedy" and takes everything it can.
If you have assurance that your tokens will (or need to) be of a specific format, you can replace .* with an appropriate character class. For example, in the likely case you want only words, you can use (\w*) in place of the (.*?) This has the advantage that closing } characters are not part of the class being matched in the inner expression, so you don't need the ? modifier).
他のヒント
試します:
\{([^}]*)\}
これは波線閉じ括弧に停止するようにブレースの内部で検索をクランプします。
別の解決策ます:
(?<=\{)([^\}]+)(?=\})
ブラケットは全く消費されないように、これは先読みと後読みを使用します。
中括弧は正規表現で特別な意味を持っているので、あなたはそれらをエスケープする必要があります。それらを一致させるために\{
と\}
を使用してください。
所属していません StackOverflow