英文:
How to reduce code's margin (=number of spaces) using Regex?
问题
最近在Notepad++
上,我想要减少行首的空格数量。
我搜索了一个正则表达式,但没有找到合适的。
在Notepad++
的文本中,我有以下这些行:
<html>
<body>
<p>ligne normale</p>
<p>ligne with 2 spaces </p>
我想要减少(即将前导的空格数量减半),仅限于所有行中'<'字符之前的空格。
期望的结果如下:
<html>
<body>
<p>ligne normale</p>
<p>ligne with 2 spaces </p>
我尝试了以下正则表达式和字符串替换:
搜索:\s{2}(?<=^\s*)
替换:
(一个空格)
但是Notepad++
拒绝了这个正则表达式!
后来,我在一个使用PCRE版本7.8的C++程序上测试了相同的正则表达式。它表明,回顾后面的字符串只能包含固定长度的内容!
有没有能够完成这个任务的正则表达式?
附注:下面的正则表达式使用前瞻部分地完成了任务...部分地!
\s{2}(?=(\s{2})*<)
最后一行中倒数第二个</p>之前的两个空格不在边距内,被删除了,但它们应该被保留!
英文:
Recently, on Notepad++
, I want to reduce number of spaces at begin of lines.
I searched a Regular Expression
, but I don't find any.
On a array(=tab) of Notepad++
, I have following lines
<html>
<body>
<p>ligne normale</p>
<p>ligne with 2 spaces </p>
and I want to reduce (= divide by 2 number of spaces) only spaces before '<' character on all lines.
Waiting result is following
<html>
<body>
<p>ligne normale</p>
<p>ligne with 2 spaces </p>
I tried following Regex
and string replacement
Search: \s{2}(?<=^\s*)
<br/>
Replace:
(= one space)
but Notepad++
refuses this Regex
!
I have then tested the same Regex
on a C++ program that is using PCRE version 7.8.
It indicates that look-behind can only contain fix length string !
Is there a Regex that can do the job ?
PS: the following Regex that use a look-ahead to the job ... partially !
\s{2}(?=(\s{2})*<)
The last 2 spaces in last line, just before last </p> are not in the margine and are removed while they must be kept !
答案1
得分: 1
Find what: ^(\s+)\1
Replace with: $1
英文:
I have not tried it on Notepad++, but this only uses very basic regular expression features in order to halve the number of start-of-line spaces:
Find what: ^(\s+)
Replace with: $1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论