与模式匹配的正则表达式,特定匹配除外

Regex Expression that matches a pattern except for specific matches

我正在尝试使用 RegEx 从 HTML 响应中提取名称。到目前为止,我能够做到这一点,除非我得到的比我需要的多,我不想匹配特定的字符串,例如 "Date" 或 "Today".

这是我的正则表达式:

<li class="rcbItem">([^:()]{1,20})<\/li>

这里是 HTML 响应的一部分,我 运行 的模式是:

            <div class="rcbSlide" style="z-index:6000;">
                <div id="ctl07_ddInspector_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Default " style="float:left;display:none;">
                    <div class="rcbScroll rcbWidth" style="width:100%;">
                        <ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;">
                            <li class="rcbItem">...Default Inspector (if any)</li>
                            <li class="rcbItem">Andy Schene</li>
                            <li class="rcbItem">gilberto hubner</li>
                            <li class="rcbItem">Jim Tinner</li>
                            <li class="rcbItem">Kenneth Donovan</li>
                            <li class="rcbItem">RENTAL REG INSPECTORS</li>
                            <li class="rcbItem">Rob Barker</li>
                            <li class="rcbItem">Robert Costello</li>
                            <li class="rcbItem">Ryan BalFour</li>
                            <li class="rcbItem">Sean Angeley</li>
                            <li class="rcbItem">Krissy King</li>
                        </ul>
                    </div>
                </div>
            </div>
            <input id="ctl07_ddInspector_ClientState" name="ctl07_ddInspector_ClientState" type="hidden" />
        </div>
        <span id="ctl07_lblInspector" class="pnlData"/>
    </td>
</tr>
<tr>
    <td>
        <span id="ctl07_lblSetDefaultLabel" class="pnlLabelLight">Set Default</span>
    </td>
    <td>
        <div id="ctl07_ddSetDefault" class="RadComboBox RadComboBox_Default pnlDD" style="width:175px;">
            <table summary="combobox" border="0" style="border-width:0;border-collapse:collapse;table-layout:fixed;width:100%">
                <tr class="rcbReadOnly">
                    <td class="rcbInputCell rcbInputCellLeft" style="margin-top:-1px;margin-bottom:-1px;width:100%;">
                        <input name="ctl07$ddSetDefault" type="text" class="rcbInput radPreventDecorate" id="ctl07_ddSetDefault_Input" value="Next Day (Default)" style="display: block;" readonly="readonly" />
                    </td>
                    <td class="rcbArrowCell rcbArrowCellRight" style="margin-top:-1px;margin-bottom:-1px;">
                        <a id="ctl07_ddSetDefault_Arrow" style="overflow: hidden;display: block;position: relative;outline: none;">select</a>
                    </td>
                </tr>
            </table>
            <div class="rcbSlide" style="z-index:6000;">
                <div id="ctl07_ddSetDefault_DropDown" class="RadComboBoxDropDown RadComboBoxDropDown_Default " style="float:left;display:none;">
                    <div class="rcbScroll rcbWidth" style="width:100%;">
                        <ul class="rcbList" style="list-style:none;margin:0;padding:0;zoom:1;">
                            <li class="rcbItem">Next Day (Default)</li>
                            <li class="rcbItem">Today</li>
                            <li class="rcbItem">Specified Date</li>
                            <li class="rcbItem">Next Available (Insp Cap)</li>
                            <li class="rcbItem">No Specified Date</li>
                        </ul>
                    </div>
                </div>
            </div>

我知道负面前瞻和另一个 post:。但我不能让它为我工作。以下仍然带回不需要的匹配项:

<li class="rcbItem">((?!Date$)[^:()]{1,20})<\/li>

由于'Date'或'Today'的位置不固定, 所以这是我的建议:

string[] filter = {"date","today"};
var result = Regex.Matches(yourhtml,"(?i) <li class=\"rcbItem\">([^:()]{1,20})<\/li>")
.Cast<Match>()
.Where(m=>!filter.Any(f=>m.Groups[1].Value.ToLower().Contains(f)));

您不能使用 $,因为它表示整个字符串的结尾。

试试这个:

<li class="rcbItem">((?!(.)*(Date|Today))[^:()]{1,20})<\/li>
  1. 你不需要 Date$ 因为 $ 意味着 "This is the very end of the string."
  2. 由于您的示例显示包含 "Date" 的不需要的字符串出现在 </li> 之前,您希望将负面环视放在那里,而不是更早。
  3. 现在,由于您想向后看以找到 Date,所以您需要向后看,而不是向前看。 (在 ?! 中间添加 <

这最终看起来像:

<li class="rcbItem">([^:()]{1,20})(?<!Date)<\/li>

如果您还想删除 "Today" 个条目,请将其设为 Today|Date:

<li class="rcbItem">([^:()]{1,20})(?<!Today|Date)<\/li>

P.S.: 你真的需要检查开头的所有空格吗?