我需要从字符串中获取所有子字符串。
例如:
StringParser.GetSubstrings("[start]aaaaaa[end] wwwww [start]cccccc[end]", "[start]", "[end]");
返回 2 个字符串“aaaaaa”和“cccccc” 假设我们只有一层嵌套。 不确定正则表达式,但我认为它会很有用。
最佳答案
private IEnumerable<string> GetSubStrings(string input, string start, string end)
{
Regex r = new Regex(Regex.Escape(start) + "(.*?)" + Regex.Escape(end));
MatchCollection matches = r.Matches(input);
foreach (Match match in matches)
yield return match.Groups[1].Value;
}
关于c# - 查找两个字符串之间的所有子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2548372/