2010-07-28 107 views
1

我有一堆字符串,看起來像這樣:'../DisplayPhotod6f6.jpg?t=before&tn=1&id=130',我想拿出問號後的所有東西,看起來像'../DisplayPhotod6f6.jpg'小vim正則表達式

s/\(.\.\.\/DisplayPhoto.\{4,}\.jpg\)*'/\1'/g 

這個正則表達式捕獲一些但不是全部的出現,你能明白爲什麼嗎?

+2

你能給一個錯過的例子嗎? – 2010-07-28 19:02:40

+0

Amardeep,它錯過了''../DisplayPhotocef3.jpg?t=before & tn = 1 & id = 54'',''../DisplayPhotod6f6.jpg?t=before & tn = 1 & id = 130'' , 還有很多。 – nnyby 2010-07-28 19:11:42

+0

第一個「。」的用途是什麼?在組的開始?它可以替換爲「'」(單引號)嗎? – matias 2010-07-28 20:21:29

回答

1

我會用宏,有時比正則表達式(和互動)簡單:

qa 
/DisplayPhoto<Enter> 
f?dt' 
n 
q 

然後一些@a,或[email protected]去,雖然所有行。

4

\.\{4,}正試圖匹配4個或更多.個字符。看起來像你想要的是「匹配任何字符4或更多」(.\{4,}),但「匹配4個或更多非.字符」([^.]\{4,})可能更準確。由於*目前適用於整個\(\)組,因此您還需要將該模式末尾的唯一*更改爲.*

+0

哦,哦..謝謝! – nnyby 2010-07-28 19:08:16

+0

奇怪它仍然不工作''../DisplayPhotocef3.jpg?t=before & tn = 1 & id = 54'' – nnyby 2010-07-28 19:10:46

1

下面的正則表達式:/(\.\./DisplayPhoto.*\.jpg)/gi

對下面的例子測試:

../DisplayPhotocef3.jpg?t=before&amp;tn=1&amp;id=54 
../DisplayPhotod6f6.jpg?t=before&amp;tn=1&amp;id=130 

將導致:

../DisplayPhotocef3.jpg 
../DisplayPhotod6f6.jpg 
+0

這是PCRE,它不同於Vim的正則表達式。 – jamessan 2010-07-29 03:32:54

3

我覺得去這個easyest方法是:

s/?.*$/'/g 

這就是說:刪除問號後面的所有內容,並用單引號替換它。

0
%s/\('\.\.\/DisplayPhoto\w\{4,}\.jpg\).*'/\1'/g 

一些注意事項:

  • %將導致掉在所有行工作。
  • \ w而不是'。',以防萬一有一些格式不正確的文件名。
  • 替換'。'在匹配正則表達式開始時,'正是它應該匹配的東西。