我需要过滤输入,以仅在括号内获取字符串:Test1,Test2,Test3。我已经尝试过,但是没有用。
string input = "test test test @T(Test1) sample text @T(Test2) Something else @T(Test3) ";
string pattern = @"[@]";
string[] substrings = Regex.Split(input, pattern);
最佳答案
您可以改用简单匹配。
(?<=@T\().*?(?=\))
string strRegex = @"(?<=@T\().*?(?=\))";
Regex myRegex = new Regex(strRegex, RegexOptions.None);
string strTargetString = @"test test test @T(Test1) sample text @T(Test2) Something else @T(Test3) ";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
// Add your code here
}
}