我想删除特定标签之间的每两个或更多空格并只留下一个空格:
例如:
<p class="text_obisnuit"> The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you. </p>
我的愿望输出:
<p class="text_obisnuit">The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.</p>
我尝试了一些方法,但没有效果
(?<=<p class="text_obisnuit">)\s*|\s*(?=</p>)
答案1
这只会删除 2 个或更多空格里面 <p class="text_obisnuit">
并</p>
保留其他多个空格。
- Ctrl+H
- 找什么:
(?:<p class="text_obisnuit">|\G)(?:(?!</p>).)*?\s\K\s+
- 用。。。来代替:
LEAVE EMPTY
- 检查环绕
- 检查正则表达式
- 请勿检查
. matches newline
取决于您是否要匹配多行。 - Replace all
解释:
(?: # start non capture group
<p class="text_obisnuit"> # literally
| # OR
\G # restart from position of last match
) # end group
(?: # start non capture group
(?!</p>) # negative lookahead, make sure we haven't reach </p>
. # any character
)*? # group may appear 0 or more times, not greedy
\s # a space
\K # forget all we have seen until this position
\s+ # 1 or more spaces
给定文本:
other text
<p class="text_obisnuit"> The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you. </p>
other text
给定示例的结果:
other text
<p class="text_obisnuit"> The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you. </p>
other text
笔记:它在紧接着<p...>
和紧接着之前保留空间</p>
如果您想删除这些空格,您必须运行另一个正则表达式:
- Ctrl+H
- 找什么:
(?<=<p class="text_obisnuit">)\s+|\s+(?=</p>)
- 用。。。来代替:
LEAVE EMPTY
- 取消勾选匹配大小写
- 检查环绕
- 检查正则表达式
- Replace all
解释:
(?<= # start positive lookbehind, make sure we have
<p class="text_obisnuit"> # literally
) # end lookbehind
\s+ # 1 or more spaces
| # OR
\s+ # 1 or more spaces
(?= # start positive lookahead
</p> # literally
) # end lookahead
给定示例的结果:
other text
<p class="text_obisnuit">The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.</p>
other text
答案2
答案3
找到解决方案:
搜索:
(?s)(\G|<p class="text_obisnuit">)((?!</p>).)*?\K((?<=>)\h+|\h+(?=<|\h))
替换为:
(leave empty)