如何编写正则表达式将HTML字符串分成几部分?

时间:2011-05-06 22:57:40

标签: c# regex

我有这个字符串:

This is sample <p id="short"> the value of short </p> <p id="medium"> the value of medium </p> <p id="large"> the value of large</p>

我想分成3件:

  • p标签之前的字符串:this is sample
  • 短:the value of short
  • medium:the value of medium
  • large:the value of large

6 个答案:

答案 0 :(得分:4)

如果您不介意非正则表达式解决方案(因为HTML不是常规语言),您可以使用此

string input = @"This is sample <p id=""short""> the value of short </p> <p id=""medium""> the value of medium </p> <p id=""large""> the value of large</p>";


string before = input.Substring(0, input.IndexOf("<"));
string xmlWrapper = "<html>" + input.Substring(input.IndexOf("<")) + "</html>";
XElement xElement = XElement.Parse(xmlWrapper);

var shortElement =
    xElement.Elements().Where(p => p.Name == "p" && p.Attribute("id").Value == "short").SingleOrDefault();
var shortValue = shortElement != null ? shortElement.Value : string.Empty;

var mediumElement =
    xElement.Elements().Where(p => p.Name == "p" && p.Attribute("id").Value == "medium").SingleOrDefault();
var mediumValue = shortElement != null ? shortElement.Value : string.Empty;

var largelement =
    xElement.Elements().Where(p => p.Name == "p" && p.Attribute("id").Value == "large").SingleOrDefault();
var largeValue = shortElement != null ? shortElement.Value : string.Empty;

答案 1 :(得分:3)

这是我的抨击:

var regex = new Regex("(?<text>.*?)<p.*?>(?<small>.*?)</p>.*<p.*?>(?<medium>.*?)</p>.*.*<p.*?>(?<large>.*?)</p>.*");
var htmlsnip = @"This is sample <p id=""short""> the value of short </p> <p id=""medium""> the value of medium </p> <p id=""large""> the value of large</p>";

var match = regex.Match(htmlsnip);
var text = match.Groups["text"].Value;
var small = match.Groups["small"].Value;
var medium = match.Groups["medium"].Value;
var large = match.Groups["large"].Value;

答案 2 :(得分:2)

(?<string_before_p_tags>[^<]*)<p id="short">(?<short>.*)</p>\s*<p id="medium">(?<medium>.*)</p>\s*<p id="large">(?<large>.*)</p>

返回指定的捕获组:

  

string_before_p_tags:这是样本
  short:短期的价值   medium:媒介的价值   large:大值

答案 3 :(得分:1)

基于Bala R的回答,这里有一个更简洁的方法来使用XPath:

string input = @"This is sample <p id=""short""> the value of short </p> <p id=""medium""> the value of medium </p> <p id=""large""> the value of large</p>";
var xmlWrapper = "<html>" + input + "</html>";
var elements = XElement.Parse(xmlWrapper).XPathSelectElements("/*").ToList();

var text = elements[0].PreviousNode.ToString();
var small = elements[0].Value;
var medium = elements[1].Value;
var large = elements[2].Value;

答案 4 :(得分:0)

首先,这里有很多次说你不应该使用正则表达式来解析html,原因有多种(主要是html不是常规语言),你应该使用HTML解析器。

但是,如果对于无法使用HTML解析器的任何限制,您可以执行以下操作:

1. string before p tags - \w[^<]

2. short - <p id="short"> [\w|\s]* [^<]

3. medium - <p id="medium"> [\w|\s]* [^<]

4. large - <p id="large"> [\w|\s]* [^<]

干杯。

答案 5 :(得分:0)

使用HtmlAgilityPack非常简单:

 string html = "This is sample <p id=\"short\"> the value of short </p> <p id=\"medium\"> the value of medium </p> <p id=\"large\"> the value of large</p>";
            string id = null;
            NameValueCollection output = new NameValueCollection();
            string[] pIds = new string[3] { "short", "medium", "large" };
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(html);
            int c = 0;
            int len = pIds.Length;
            while (c < len)
            {
                id = pIds[c];
                output.Add(id, doc.GetElementbyId(id).InnerHtml);
                c++;
            }

       //In key of output variable, is equivalent to value of paragraph. example:
        Console.WriteLine(output["short"].ToString()); 

输出:the value of short