<created>
[email protected]
</created>
我要替換以上,但用戶名可能會有所不同,即,拍拍@ c.com,哈利@ c.com ...VIM更換一個模式
<created>
[email protected]
</created>
什麼命令在vim替換該
%s/<created>\r*\r</created>/new string
<created>
[email protected]
</created>
我要替換以上,但用戶名可能會有所不同,即,拍拍@ c.com,哈利@ c.com ...VIM更換一個模式
<created>
[email protected]
</created>
什麼命令在vim替換該
%s/<created>\r*\r</created>/new string
這取決於你想要的格式,但你引用具體的例子,你可以做下列操作之一:
真的簡單,無需上下文:與[email protected]
:%s/[email protected]\.com/[email protected]/g
取代[email protected]隨着上下文
:%s:<created>\s*\n\s*\zs.\{-}\[email protected]\.com\s*\n\s*</created>:tom
作爲解釋:
:%s:XXX:YYY - Substitute XXX with YYY over the whole file (using colons as delimiters to avoid having to escape slashes or @s with a backslash)
where XXX is:
<created> - literal text to search for
\s* - soak up any spaces or tabs to the end of the line
\n - a new line character
\s* - soak up any spaces or tabs at the start of the line
\zs - special code that says "the match starts here", so only the bit between this and the \ze are actually replaced
.\{-} - catch any characters, as few as possible - this will match 'pat' in the example above
\ze - end of the bit we're changing
@c - literal text - the @ and the start of the domain name
\. - '.' means any character, so to match a literal dot, you must escape it
com - literal text - the end of the email address
\s* - any spaces/tabs to the end of the line
\n - a new line character
\s* - any spaces/tabs
</created> - literal match on the terminator (works because we don't use '/' as the delimiters)
and YYY is just the literal string "tom" to insert
另一種形成:
:%s:<created>\_s*\zs\S\+\ze\_s*</created>:[email protected]
:%s:XXX:YYY: - as before
where XXX is:
<created> - literal text to search for
\_s* - search for zero or more white-space characters, including new-lines (hence the underscore)
\zs - as before, this is the start of the bit we want to replace (so we're not changing the <created> bit)
\S\+ - one or more non-whitespace characters (\+ is one or more, * is zero or more) - this should catch the whole email address
\ze - as before, the end of the match
\_s* - zero or more white-space characters
</created> - the end delimiter
YYY is then the whole email address.
我希望臨時噸給你一些有用的信息。正則表達式上的正則表達式有很多有用的參考指南(這些就是這些指南)(儘管請注意Vim使用的格式與大多數不同:\+
而不是+
等)。我強烈推薦閱讀:
:help pattern.txt
但請記住,那裏有很多,所以請逐步閱讀並試驗。您也可以先使用簡單的搜索(按/
)並考慮稍後進行替換,例如,類型:
/<created>\_s*\zs\S\+\ze\_s*<\/created>
請注意,我用一個反斜槓前綴/
作爲檢索開始是/
。一個聰明的技巧是:s
默認使用最後一次搜索,所以你可以輸入上面的行(/<created>\_s*\zs\S\+\ze\_s*<\/created>
)並調整它直到它是正確的,然後只是做:%s::[email protected]
並且由於上面標記爲XXX的位缺失,它將使用你最後的搜索,只是工作!
如果上面有任何位不理解,:help
是你的朋友。例如,要了解\zs
,類型:
:help \zs
的信息有關\_s
,類型:
:help \_s
的一般信息有關:s
類型:
:help :s
等..
哇,我正在鍵入我的答案,但鋁在這裏搞定了。 +1。乾杯。 – sleepynate 2010-08-06 13:10:14
謝謝......誓言...... – Hulk 2010-08-07 04:37:48