次を含むSQLテキストがあります
"Select * from Table Where [PARAM1] = [PARAM2] ..."
"[" "]"
リストするタグ間のリストを取得したい。
どうすればできますか?
LINQを使用してそれを行うことができます
string str = "Select * from Table Where [PARAM1] = [PARAM2] ...";
string[] Array = str.Split().Where(r=> r.StartsWith("[") && r.EndsWith("]"))
.Select(r=> r.Trim('[',']'))
.ToArray();
正規表現といくつかの 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
このスニペットを使用
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
}
あなたもすることができます
List<string> matches=Regex.Matches(@"(?<=\[)[^\[\]]*(?=\])")
.Cast<Match>()
.Select(x=>x.Value)
.ToList();
必要なものは次のとおりです。
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....
}
MatchCollection を List に変換するには、次を使用します。
List<Match> myMatches = Regex.Matches("bla", @"\[[^]]*)\]").Cast<Match>.toList();
次の正規表現を試してみてください。
Regex regex = new Regex(@"\[.*?\]");
var parameters = regex.Matches(sqlQueryString);