Answers:
Vim的正则表达式对非贪婪版本的运算符具有特殊的语法(这很烦人,但您只需要记住它们即可):http : //vimregex.com/#Non-Greedy
的非贪婪版本*
为\{-}
。因此,只需替换.*
为.\{-}
:
:%s/\(https\?:\/\/.\{-}\/\).*/\1/gc
您也可以使用[^\]+/.
来防止贪婪。[^/]
表示“匹配任何期望的内容/
,并+
重复一次或多次。”
:%s!\v^(https?)\://([^/]+)/.*$!Protocol:\1 \t Domain:\2!g
如果我使用/
了正则表达式,则将其!
用作分隔符,这样就不必逃脱了/
。
假设您有以下网址:
http://academy.mises.org/courses/econgd/
http://academy.mises.org/moodle/course/view.php?id=172
http://acmsel.safaribooksonline.com/book/-/9781449358204?bookview=overview
http://acmsel.safaribooksonline.com/home
http://acordes.lacuerda.net/bebo__cigala/lagrimas_negras-2.shtml
http://acordes.lacuerda.net/jose_antonio_labordeta/albada.shtml
http://anarchitext.wordpress.com/category/new-middle-east/
https://courses.edx.org/courses/course-v1%3ADelftX%2BFP101x%2B3T2015/wiki/DelftX.FP101x.3T2015/resources-and-links/
https://cseweb.ucsd.edu/classes/wi11/cse230/lectures.html
https://developer.mozilla.org/en-US/docs/CSS
https://developers.google.com/edu/python
https://developers.google.com/structured-data/testing-tool/
应用替换,您将得到:
Protocol:http Domain:academy.mises.org
Protocol:http Domain:academy.mises.org
Protocol:http Domain:acmsel.safaribooksonline.com
Protocol:http Domain:acmsel.safaribooksonline.com
Protocol:http Domain:acordes.lacuerda.net
Protocol:http Domain:acordes.lacuerda.net
Protocol:http Domain:anarchitext.wordpress.com
Protocol:https Domain:courses.edx.org
Protocol:https Domain:cseweb.ucsd.edu
Protocol:https Domain:developer.mozilla.org
Protocol:https Domain:developers.google.com
Protocol:https Domain:developers.google.com
:help greedy
本来可以带您到正确的帮助主题。:help regexp
是描述Vim的正则表达式风味的帮助。