正则表达式解析HTML

时间:2009-09-22 20:19:31

标签: c# regex

我的输入字符串是:

"<!--<clientHtml>--><br><br><br><b>Job Title:</b> Test text
<br><b>JobId:</b> 56565-116503
<br><br><b>City:</b> San Diego
<br><b>State:</b> CA
<br><b>Zip Code:</b> 92108
<br><br><br><b>Description:</b> 
            We are recruiting for a Controller to oversee all accounting and finance for a growing manufacturing company.  We are looking for someone who is hands on full cycle accounting.  


<br><br>
<!--<apply>test/apply><email></email><OriginalFetchUrl>http:test.xml</OriginalFetchUrl><OriginalWrapUrl>http://test.html</OriginalWrapUrl></clientHtml>-->";

我需要使用C#/正则表达式提取以下字符串:

1.“我们正在招聘一名财务总监,负责监管一家不断发展的制造公司的所有会计和财务。我们正在寻找一位负责全周期会计的人。”

我也想摆脱这条线: 测试/应用&GT;&LT; /电子邮件&GT; HTTP:的test.xml&LT; / OriginalFetchUrl&GT; HTTP://test.html< / OriginalWrapUrl&GT;&LT; / clientHtml&GT; - &GT;

我可以请求代码帮助吗?

感谢阅读。

2 个答案:

答案 0 :(得分:2)

尝试这样的事情:

  

Description:</b>([^<]+)

以下是如何使用它的示例:

using System;
using System.Text.RegularExpressions;

class Example
{
    static void Main()
    {
        String str = @"<!--<clientHtml>--><br><br><br><b>Job Title:</b> Test text
            <br><b>JobId:</b> 56565-116503
            <br><br><b>City:</b> San Diego
            <br><b>State:</b> CA
            <br><b>Zip Code:</b> 92108
            <br><br><br><b>Description:</b> 
                    We are recruiting for a Controller to oversee all accounting and finance for a growing manufacturing company.  We are looking for someone who is hands on full cycle accounting.  


            <br><br>
            <!--<apply>test/apply><email></email><OriginalFetchUrl>http:test.xml</OriginalFetchUrl><OriginalWrapUrl>http://test.html</OriginalWrapUrl></clientHtml>-->";

        Regex expression = new Regex(@"Description:</b>([^<]+)",
            RegexOptions.Compiled |
            RegexOptions.CultureInvariant |
            RegexOptions.IgnoreCase);

        Match match = expression.Match(str);

        if (match.Success)
            Console.WriteLine(match.Groups[1].Value.Trim());
    }
}

答案 1 :(得分:0)

尝试这样的事情:(我没有测试过。)

string result = "";
Match m = Regex.Match(line, @"^\<b\>\s*Description\s*\:\s*\<\/b\>\s*(?<result>.*?)\s*\<", RegexOptions.IgnoreCase);
if (m.Success) 
{
    result = m.Groups["result"].Value;
}