-1

次を含むSQLテキストがあります

"Select * from Table Where [PARAM1] = [PARAM2] ..."

"[" "]"リストするタグ間のリストを取得したい。

どうすればできますか?

4

7 に答える 7

4

LINQを使用してそれを行うことができます

string str = "Select * from Table Where [PARAM1] = [PARAM2] ...";
string[] Array = str.Split().Where(r=> r.StartsWith("[") && r.EndsWith("]"))
                    .Select(r=> r.Trim('[',']'))
                    .ToArray();
于 2013-01-17T09:14:07.950 に答える
2

正規表現といくつかの LINQ を使用してみることができます。

Regex t = new Regex(@"\[([^]]*)\]");
List<String> parameters = t.Matches(input_string).Cast<Match>().Select(a => a.Groups[1].ToString()).ToList();

これにより、2 つの一致PARAM1を保持する List が生成され、PARAM2

于 2013-01-17T09:12:12.690 に答える
2

このスニペットを使用

string strRegex = @"\[(.*?)\]";
RegexOptions myRegexOptions = RegexOptions.IgnoreCase;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"Select * from Table Where [PARAM1] = [PARAM2] ...";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
   // myMatch.Groups[0] - your string
}
于 2013-01-17T09:15:24.063 に答える
1

あなたもすることができます

List<string> matches=Regex.Matches(@"(?<=\[)[^\[\]]*(?=\])")
                          .Cast<Match>()
                          .Select(x=>x.Value)
                          .ToList();
于 2013-01-17T09:19:49.177 に答える
1

必要なものは次のとおりです。

Regex t = new Regex(@"\[(.*?)\]");

string str = @"Select * from Table Where [PARAM1] = [PARAM2] ...";

foreach (Match myMatch in myRegex.Matches(str))
{
   // myMatch.Groups[0] and so on....
}

ライブデモ

于 2013-01-17T09:20:41.287 に答える
0

MatchCollection を List に変換するには、次を使用します。

List<Match> myMatches = Regex.Matches("bla", @"\[[^]]*)\]").Cast<Match>.toList();
于 2013-01-17T09:14:41.220 に答える
0

次の正規表現を試してみてください。

Regex regex = new Regex(@"\[.*?\]");
var parameters = regex.Matches(sqlQueryString);
于 2013-01-17T09:15:14.727 に答える