我一直在試圖使一個正則表達式能夠匹配「任何」,但一定的標記,我正在按照這個答案(Match everything except for specified strings),但它根本不工作...正則表達式能夠匹配任何東西,但特定的標記
下面是一個例子
text = '<a> whatever href="obviously_a_must_have" whatever <div> this div should be accepted </div> ... </a>'
regex = r'<a[^><]*href=\"[^\"]+\"(?!.*(</a>))*</a>' #(not working as intended)
[^><]* #- should accept any number of characters except <and>, meaning it shouldn't close the tag nor open a new one - *working*;
href=\"[^\"]+\" #- should match an href - *working*;
(?!.*(</a>))* #- should match anything but the end of the tag a - *NOT WORKING*.
什麼不工作? [請注意,正則表達式不是解析html的正確工具。](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 ) –
嘿@HåkenLid,正則表達式的第三部分沒有按預期工作。別擔心!我解析html的合法方式;)但我仍然必須使用這個正則表達式匹配某些標準,因爲我不想要所有類型的標籤,只是某些標籤。這只是一個例子! –
由於該組無法量化,因此不能在'(?!)'負向預讀組之後直接放置* *量詞。嘗試在線編輯器中的代碼[立即顯示](https:// regex101。COM/R/qAlpvE/1)。我並不是100%確定你想用這個'*'量詞來做什麼? –