HTML 查找某文字列 并读取其注释内容 用正则好吗?

sosoben 2013-08-05 09:19:45
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='ocr-system' content='tesseract 3.02' />
<meta name='ocr-capabilities' content='ocr_page ocr_carea ocr_par ocr_line ocrx_word'/>
</head>
<body>
<div class='ocr_page' id='page_1' title='image "tamron.new.exp4.TIF"; bbox 0 0 547 70; ppageno 0'>
<div class='ocr_carea' id='block_1_1' title="bbox 28 27 509 54">
<p class='ocr_par' dir='ltr' id='par_1' title="bbox 28 27 509 54">
<span class='ocr_line' id='line_1' title="bbox 28 27 509 54">
<span class='ocrx_word' id='word_1' title="bbox 28 27 76 52">200</span>
<span class='ocrx_word' id='word_2' title="bbox 108 27 200 52">135100</span>
<span class='ocrx_word' id='word_3' title="bbox 228 27 260 52">70</span>
</span>
</p>
</div>
</div>
</body>
</html>



内容如上, 我想查找其文字列“200” 的注释 title="bbox 28 27 76 52" 这一部分 应该怎么找好呢?
如果正则求正则表达式~~(不能与"bbox 108 27 200 52" 里面的200 混淆)
...全文
137 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sosoben 2013-08-08
  • 打赏
  • 举报
回复
引用 5 楼 youzelin 的回复:

string s = @"
    <?xml version=""1.0"" encoding=""UTF-8""?>
    <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
    <html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"" lang=""en"">
        <head>
            <title></title>
            <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
            <meta name='ocr-system' content='tesseract 3.02' />
            <meta name='ocr-capabilities' content='ocr_page ocr_carea ocr_par ocr_line ocrx_word'/>
        </head>
        <body>
            <div class='ocr_page' id='page_1' title='image ""tamron.new.exp4.TIF""; bbox 0 0 547 70; ppageno 0'>
                <div class='ocr_carea' id='block_1_1' title=""bbox 28 27 509 54"">
                    <p class='ocr_par' dir='ltr' id='par_1' title=""bbox 28 27 509 54"">
                    <span class='ocr_line' id='line_1' title=""bbox 28 27 509 54"">
                        <span class='ocrx_word' id='word_1' title=""bbox 28 27 76 52"">200</span> 
                        <span class='ocrx_word' id='word_2' title=""bbox 108 27 200 52"">135100</span> 
                        <span class='ocrx_word' id='word_3' title=""bbox 228 27 260 52"">70</span>
                    </span>
                    </p>
                </div>
            </div>
        </body>
    </html>
";

Regex regex = new Regex(@"\<span(?:\s+(?<kv>\w+=(?:'[^']+'|""[^""]+"")))+\>200\</span\>");
Match match = regex.Match(s);
if (match.Success)
{
    foreach (Capture capture in match.Groups["kv"].Captures)
    {
        if (capture.Value.IndexOf("title") >= 0)
        {
            Console.WriteLine(capture.Value.Substring(capture.Value.IndexOf("=") + 1));
        }
    }
}
Console.ReadKey();
你好,我的200 前面有时侯会出现个<em> 有时侯又不会,请问怎么修改才能比较完美呢?
youzelin 2013-08-05
  • 打赏
  • 举报
回复

string s = @"
    <?xml version=""1.0"" encoding=""UTF-8""?>
    <!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
    <html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"" lang=""en"">
        <head>
            <title></title>
            <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
            <meta name='ocr-system' content='tesseract 3.02' />
            <meta name='ocr-capabilities' content='ocr_page ocr_carea ocr_par ocr_line ocrx_word'/>
        </head>
        <body>
            <div class='ocr_page' id='page_1' title='image ""tamron.new.exp4.TIF""; bbox 0 0 547 70; ppageno 0'>
                <div class='ocr_carea' id='block_1_1' title=""bbox 28 27 509 54"">
                    <p class='ocr_par' dir='ltr' id='par_1' title=""bbox 28 27 509 54"">
                    <span class='ocr_line' id='line_1' title=""bbox 28 27 509 54"">
                        <span class='ocrx_word' id='word_1' title=""bbox 28 27 76 52"">200</span> 
                        <span class='ocrx_word' id='word_2' title=""bbox 108 27 200 52"">135100</span> 
                        <span class='ocrx_word' id='word_3' title=""bbox 228 27 260 52"">70</span>
                    </span>
                    </p>
                </div>
            </div>
        </body>
    </html>
";

Regex regex = new Regex(@"\<span(?:\s+(?<kv>\w+=(?:'[^']+'|""[^""]+"")))+\>200\</span\>");
Match match = regex.Match(s);
if (match.Success)
{
    foreach (Capture capture in match.Groups["kv"].Captures)
    {
        if (capture.Value.IndexOf("title") >= 0)
        {
            Console.WriteLine(capture.Value.Substring(capture.Value.IndexOf("=") + 1));
        }
    }
}
Console.ReadKey();
xiongxyt2 2013-08-05
  • 打赏
  • 举报
回复
string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt string pattern = @"<span[^>]*?class=(['""]?)ocrx_word\1[^>]*?title=(['""]?)([^'""]+)\2[^>]*?>200</span>";
sosoben 2013-08-05
  • 打赏
  • 举报
回复
引用 1 楼 Return_false 的回复:
string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
            string pattern = @"<span[^>]*?class=(['""]?)ocrx_word\1[^>]*?title=(['""]?)([^'""]+)\2[^>]*?>";

            string result = Regex.Match(tempStr, pattern).Groups[3].Value;//bbox 28 27 76 52
你好 谢谢你的回答。 已经基本能拿到bbox 28 27 76 52 这个数据,不过我看正则(我不太懂) 不过好像不是查找那个200, 而是查找第一个ocrx_word 就是我一旦200不是最前面就会 错了 请问如何修改呢?
newtee 2013-08-05
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
string tempStr = File.ReadAllText(@"C:\Users\myx\Desktop\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
            string pattern = @"<span[^>]*?class=(['""]?)ocrx_word\1[^>]*?title=(['""]?)([^'""]+)\2[^>]*?>";

            string result = Regex.Match(tempStr, pattern).Groups[3].Value;//bbox 28 27 76 52
sosoben 2013-08-05
  • 打赏
  • 举报
回复
4 楼 5 楼 都可以,5楼比较详细,如果没有更好的就结贴给分了

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧