我有一堆我正在解析的 html,<a>如果它們包含某些文本,我需要刪除它們。通常,我會使用 Goquery,但我正在搜索的文本通常不在 html 標記本身的范圍內。例如,這個 html:<html><body>This is the start. <a href="http://example.com/path">We don't want to match this text.</a><a href="http://www.example.com/another/path" style="font-family:Arial, Helvetica, 'sans-serif'; color:#838383;font-size:12px; line-height:14px"></a> match this text.<a href="blah">We also don't want to match this text</a></body></html>我正在使用這個正則表達式,但它失敗并匹配我不想匹配的文本:(?is)<a[^>]+href=["'](?P<link>.*?)["']*.?> match this text\.https://regex101.com/r/iEXpqc/1
1 回答

回首憶惘然
TA貢獻1847條經驗 獲得超11個贊
像這樣,使用路徑(不是去,但邏輯可以重新實現):
xmlstarlet ed -d '//a[contains(text(), "want to match")]' file.html
輸出
<?xml version="1.0"?>
<html>
<body>
This is the start.
<a href="http://www.example.com/another/path" style="font-family:Arial, Helvetica, 'sans-serif'; color:#838383;font-size:12px; line-height:14px"/> match this text.
</body>
</html>
筆記
-L如果要即時更換,請添加開關
- 1 回答
- 0 關注
- 176 瀏覽
添加回答
舉報
0/150
提交
取消