0

假设我有这个字符串(巨大的),我想过滤掉除了我正在寻找的东西之外的所有东西。这是我想要的一个例子:

<strong>You</strong></font> <font size="3" color="#05ABF8">
<strong>Shook</strong></font> Me All <font size="3" color="#05ABF8">
<strong>Night</strong></font> <font size="3" color="#05ABF8">
<strong>Long</strong></font> mp3</a></div>

如您所见,所有这些之间都有文字。我想得到“You Shook Me All Night Long”,然后把剩下的拿出来。我将如何完成这项工作?

4

2 回答 2

3

您可以使用以下正则表达式:>([\s|\w]+)<

var input = @"
<strong>You</strong></font> <font size='3' color='#05ABF8'>
<strong>Shook</strong></font> Me All <font size='3' color='#05ABF8'>
<strong>Night</strong></font> <font size='3' color='#05ABF8'>
<strong>Long</strong></font> mp3</a></div>";

var regex = new Regex(@">(?<match>[\s|\w]+)<");

var matches = regex.Matches(input).Cast<Match>()
   // Get only the values from the group 'match'
   // So, we ignore '<' and '>' characters
   .Select(p => p.Groups["match"].Value);

火柴

// Concatenate the captures to one string
var result = string.Join(string.Empty, matches)
    // Remove unnecessary carriage return characters if needed
    .Replace("\r\n", string.Empty);

结果

于 2012-10-28T03:11:32.293 回答
1

</a></div>假设您在发布的 xml/html 末尾有有效的开始标签。

string value = XElement.Parse(string.Format("<root>{0}</root>", yourstring)).Value;

或剥离 Html 的方法:

public static string StripHTML(this string HTMLText)
{
    var reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
    return reg.Replace(HTMLText, "").Replace("&nbsp;", " ");
}
于 2012-10-28T02:40:52.633 回答