我正在寻找一种从具有特定标签的字符串中获取字符串的方法,例如
我有这个字符串:"Hello <date> My <name> is <your name>"
我需要回复这个:
<date>
<name>
<your name>
在数组或列表中
只有单词以&lt;&gt;开头和结尾。
万分感谢! : - )
答案 0 :(得分:4)
您可以使用正则表达式模式<.*?>
来检索每个单词,即
MatchCollection matches = Regex.Matches(input, "<.*?>");
然后,您可以遍历集合以获取标记。
答案 1 :(得分:1)
Mike Precup在1分钟内击败我:)无论如何你应该使用Regular Expressions,例如:
var s = @"some <thing> is different <about> this <string>";
var pattern = @"(?<=\<)(.*?)(?=\>)";
var regex = new Regex(pattern);
var matches = regex.Matches(s);
foreach (Match match in matches)
{
match.Groups[0].Captures[0].Value.Dump(); // using LINQPad
}
,输出为:
thing
about
string
亲切的问候, P上。