2014-10-29 65 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(看比賽信息在右側面板中)

希望它能幫助。