正则表达式替换记事本++中的html空格和前导空格

时间:2011-01-13 17:17:04

标签: regex notepad++

我试图使用以下正则表达式删除html空格和前导空格

Find:   \s*([<>])\s*

Replace: $1

但每次我这样做,我最终在我的文档中出现了186个1美元的文字。非常感谢任何帮助

以下是我所谈论的一个例子

<fieldset id="prod_desc">
<p>Original AA </p>
<b>Features:</b> 
<ul>
  <li>2 pole rectangular dome tent with 13.4 sq ft of vestibule storage </li>
  <li>Durable, shockcorded, self-supporting fiberglass frame and ring and pin/pole pocket assembly </li>
  <li>2 side opening door panels are constructed entirely of no see-um mesh to maximize air flow inside </li>
  <li>Poke-out vent in side wall allows the option of additional ventilation when needed </li>
  <li>2 interior storage pockets keep essential items handy Specifications: </li>
  <li>Season: 3 </li>
  <li>Sleeps: 2 </li>
  <li>Doors: 2 </li>
  <li>Windows: 2 </li>
  <li>Weight: 5 lbs 12 oz </li>
  <li>Area: 36.5 Sq. Ft. </li>
  <li>Center Height: 3' 7.5&quot;</li>
</ul>
</fieldset> 

应该成为:

<fieldset id="prod_desc"><p>Original AA</p><b>Features:</b><ul><li>2 pole rectangular dome tent with 13.4 sq ft of vestibule storage</li><li>Durable, shockcorded, self-supporting fiberglass frame and ring and pin/pole pocket assembly</li><li>2 side opening door panels are constructed entirely of no see-um mesh to maximize air flow inside</li><li>Poke-out vent in side wall allows the option of additional ventilation when needed</li><li>2 interior storage pockets keep essential items handy Specifications:</li><li>Season: 3</li><li>Sleeps: 2</li><li>Doors: 2</li><li>Windows: 2</li><li>Weight: 5 lbs 12 oz</li><li>Area: 36.5 Sq. Ft.</li><li>Center Height: 3' 7.5&quot;</li></ul></fieldset>

2 个答案:

答案 0 :(得分:4)

Notepad ++在版本6.0之前的反向引用时不支持$1,因为它引入了PCRE对查找和替换的支持。对于旧版本,请使用\1进行反向引用。

你应该找到\s*(<[^>]+>)\s*。从2012年3月发布的Notepad ++ 6.0版开始,仅此一项就适用于您。我尝试了你的原始正则表达式,它也很有效,令我惊讶。

以前的版本无法进行多行正则表达式替换。要删除换行符,请先执行正则表达式替换,然后执行扩展查找(UNIX行结尾):

\n

对于Windows行结尾:

\r\n

用任何东西替换任何一个案例。

答案 1 :(得分:0)

您可以使用表达式\s+\<(.*)\>\s+并替换为$ 1(或Notepad ++中的\ 1)

或者你可以使用这种方法:

  • 首先,匹配\s+\<并替换为<
  • 秒,匹配\>\s+并替换为>
相关问题