2014-10-29 60 views
0

如何使用正则表达式在Notepad ++中删除特定字符串周围不需要的文本?带数字的字符串不必删除。我们需要的数字(字符串)总是被“onRemoveVariable([0-9] *)”包围。如何通过正则表达式文本去除

来源:

<table> 
<tr><td style="css"> 
<a href="#" onclick="onRemoveVariable(12354);">del</a> 
<a href="#" onclick="onEditVariable(1235446);">edit</a> 
</td></tr> 
<tr><td style="css"> 
<a href="#" onclick="onRemoveVariable(1231584);">del</a> 
<a href="#" onclick="onEditVariable(12354631);">edit</a> 
</td></tr> 

结果:

12354 
1231584 

是否有人有想法?

贝斯特认为 马里奥

回答

1

您可以使用此正则表达式来删除除onRemoveVariable部件之间的数字家居:

^.*?onRemoveVariable\((\d+)\).*$|.* 

这将尝试先拿到号码,如果没有找到,匹配整个行。

替换字符串:

$1 

如果数量匹配时,替换字符串将因此只放了多少回。如果不是,则$1将为空,结果将是空行。

regex101 demo

如果你现在要删除多个空行,你可以使用类似:

\R+ 

并将其替换:

\r\n 

然后手动删除任何剩余的空行(最多可以有2个替换,一个在开始,一个在结尾)。 \R匹配任何换行符,并且\R+因此匹配多个换行符。上述因此用单行换行取代多个换行符。


^      # Beginning of line 
    .*?     # Match everything until... 
    onRemoveVariable\( # Literal string oneRemoveVariable(is matched 
    (\d+)    # Store the digits 
    \)     # Match literal) 
    .*     # Match any remaining characters 
$      # End of line 
|      # OR if no 'onRemoveVariable(` is found with digits and)... 
    .*     # Match the whole line 
+0

哇,这是快。它完美地工作。谢谢 – marioa 2014-10-29 10:34:03

+0

@marioa不客气:) – Jerry 2014-10-29 10:34:18

1

你需要找到所有的数字\d+onRemoveVariable(之前和)后。 使用前瞻和lookbehind断言。

(?<=onRemoveVariable\()(\d+)(?=\)) 
0

您可以使用此正则表达式匹配只是你想要的数字:

/onRemoveVariable\((\d+)\)/g 

DEMO(看比赛信息在右侧面板中)

希望它能帮助。