-2

我正在寻找一种从具有特定标签的字符串中获取字符串的方法,例如我有这个字符串:"Hello <date> My <name> is <your name>" 我需要返回这个: <date> <name> <your name>

在数组或列表中

只有单词以 <> 开头和结尾。

太感谢了!:-)

4

2 回答 2

4

您可以使用正则表达式模式<.*?>来检索每个单词,即

MatchCollection matches = Regex.Matches(input, "<.*?>");

然后,您可以遍历集合以获取标签。

于 2013-05-19T18:01:35.923 回答
1

Mike Precup 比我快 1 分钟 :) 无论如何你应该使用正则表达式,例如:

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。

于 2013-05-19T18:08:13.557 回答