tags:

views:

277

answers:

2

I have following text in a text file

This is some text for cv_1 for example
This is some text for cv_001 for example
This is some text for cv_15 for example

I am trying to use regex cv_.*?\s to match cv_1, cv_001, cv_15 in the text. I know that the regex works. However, it doesn't match anything when I try it in VIM.

Do we need to do something special in VIM?

+5  A: 

The perl non-greedy "?" char doesn't work in Vim, you should use:

cv_.\{-}\s

Instead of

cv_.*?\s

Here's some quick reference for matching:

* (0 or more) greedy matching
\+ (1 or more) greedy matching
\{-} (0 or more) non-greedy matching
\{-n,} (at least n) non-greedy matching
Gerald Kaszuba
`%s/cv_.\{-}\ze\s/replacement/g`
John Kugelman
`\ze` ends the match before the whitespace character so it doesn't get replaced
John Kugelman
+3  A: 

vim's regex syntax is a little different -- what you're looking for is

cv_.\{-}\s

(the \{-} being the vim equivalent of perl's *?, i.e., non-greedy 0-or-more). See here for a good tutorial on vim's regular expressions.

Alex Martelli