0

I have a program extracting some information from a lot of large html pages. I found that the last line (myRegex.Match(detailPage)) takes most of the execution time. Is the regex pattern optimized?

const string strRegex = @"prepend-top.*?<h1[^>]*?>(?<name>.+?)\s*<a.*?
    Create\ Date.*?<label>(?<create>.*?)</label>.*?
    <a.*?id\s*=\s*""period_report"".*?href\s*=\s*""(?<url>.*?)""";
const RegexOptions myRegexOptions =
            RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled |
            RegexOptions.IgnorePatternWhitespace;
var myRegex = new Regex(strRegex, myRegexOptions);
var m = myRegex.Match(detailPage);

The HTML code looks like (The html file size is about 30K, however, most of the the html is javascript code):

<div class="span-24 prepend-top">
<h1>XXX XXX XXXX 
    <a href="https://....">Back to Search Results</a></h1>
</div>

<div class="span-18">
<div class="top-content">

<script type="text/javascript">
 .....
</script>

    <div class="detailHeaderContainer">
        <div class="leftBlock">

            <div class="left staticlabel leftStaticlabelWidth inlineColumn">
                <label>
                    Product Type:
                </label>
            </div>
            <div class="left leftDynamiclabelWidth dynamiclabel">
                <label>Type 2</label>
            </div>
            <div class="clear"></div>

            <div class="left staticlabel leftStaticlabelWidth inlineColumn">
                <label>
                    Create Date:
                </label>
            </div>
            <div class="left leftDynamiclabelWidth dynamiclabel">
                <label></label>
            </div>
4

2 回答 2

0

我建议快速查看MSDN 上的正则表达式最佳实践指南BCL 团队博客上的此博客条目,它们深入了解正则表达式的行为,并可以就正则表达式为何会变慢提供指导

于 2012-08-17T20:07:27.200 回答
0

创建静态 RegEx 类可以节省大量时间。

于 2012-12-21T22:49:15.117 回答