关于vim正则表达式的重要事项是需要转义不同的级别(与Perl或Ruby中的正则表达式相反)
从 :help /\m
after: \v \m \M \V matches 'magic' 'nomagic' $ $ $ \$ matches end-of-line . . \. \. matches any character * * \* \* any number of the previous atom () \(\) \(\) \(\) grouping into an atom | \| \| \| separating alternatives \a \a \a \a alphabetic character \\ \\ \\ \\ literal backslash \. \. . . literal dot \{ { { { literal '{' a a a a literal 'a'
默认设置是'magic',所以为了使你给的正则表达式有效,你必须使用:
:%s/".*\(\d\{10}\).*"/\1/
如果你想删除除前7位数字和匹配之外的所有内容(我假设你的意思是你想要删除没有任何匹配的行)
:v/^\([[:alnum:]]\{7}\),\s*".*\(\d\{10}\).*/d :%s//\1,\2/
该:v/
命令允许您在每条与给定模式不匹配的行上运行命令,因此这只会删除不匹配项. :s//
重用先前的模式,因此我们不必指定它.
这改变了以下内容:
0g98932,"long description sometimes containing numbers like 0123456789" 0g98932,"long description no numbers" 0g98932,"long description no numbers" 0g98932,"long description sometimes containing numbers like 0123456789" 0g98932,"long description no numbers" 0g98932,"long description no numbers" 0g98932,"long description no numbers" 0g98932,"long description no numbers" 0g98932,"long description sometimes containing numbers like 0123456789" 0g98932,"long description no numbers" 0g98932,"long description no numbers" 0g98932,"long description sometimes containing numbers like 0123456789"
进入这个:
0g98932,0123456789 0g98932,0123456789 0g98932,0123456789 0g98932,0123456789