简体   繁体   中英

Regex Expression that matches a pattern except for specific matches

I'm trying to extract Names from a HTML Response using a RegEx. So far I'm able to do so except I'm getting more than I need, I want to NOT match specific strings such as "Date" or "Today".

Here is my regex:

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

And here is a portion of the HTML response against which I'm running the pattern:

            <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>

I'm aware of negative lookaheads and this other post: Regex that matches a pattern and doesn't match specific words . But I can't make that work for me. The following still brings back the unwanted matches:

<li class="rcbItem">((?!Date$)[^:()]{1,20})<\/li>
  1. You don't want Date$ because the $ means "This is the very end of the string."
  2. Since your examples show that the unwanted strings containing "Date" have it appearing right before </li> , you want to put your negative lookaround right there, not earlier.
  3. Now, since you want to be looking backward to find Date, you want a lookbehind, not a lookahead. (add < in the middle of your ?! )

This ends up looking like:

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

If you're also trying to remove "Today" entries, then make it Today|Date :

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

PS: Do you really need to check for all that whitespace at the beginning?

As the location of 'Date' or 'Today' is not fixed, so here is my suggestion:

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)));

You cannot use the $ because it indicates the end of the entire string.

Try this one:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM