我有很多 html 文件,我必须使用正则表达式进行搜索和替换。因此,我必须选择一个特定标签<span class="text_obisnuit">text<\p>
并将其替换为另一个标签。
例如:
<p><span class="text_obisnuit">My text here.</span></p>
应该变成:
<p class="text_obisnuit">My text here.</p>
答案1
- Ctrl+H
- 找什么:
<p\K><span([^>]*?class="text_obisnuit"[^>]*>.*?)</span>(?=</p)
- 用。。。来代替:
$1
- 查看 环绕
- 查看 正则表达式
- Replace all
解释:
<p # literally
\K # forget all we have seen until this position
><span # literally
( # start group 1
[^>]*? # 0 or more any character that is not >, not greedy
class="text_obisnuit" # literally
[^>]* # 0 or more any character that is not >
> # literally
.*? # 0 or more any character, not greedy
) # end group 1
</span> # literally
(?=</p) # positive lookahead, make sure we have </p> after
替代品:
$1 # content of group 1, the class and the text
屏幕截图(之前):
屏幕截图(之后):
答案2
查找 =(?s)<p><span (.*?class="text_obisnuit".*?)>(.*?)</span></p>
更换 =<p $1>$2</p>
MODE = 正则表达式
来源:https://community.notepad-plus-plus.org/topic/18719/regex-replace-tag-with-another-tag-from-text/5