Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to find specific length numbers in a big document. I tried to use regex for this. For example, If I need to search for numbers with exactly 2 digits, I use /d/d (i.e. /d twice followed by a space). This works well.

But for finding 10 digit numbers it's not really feasible to type in /d 10 times.

Tried /d{2}, says 'E486: Pattern not found: \d{2}'

Is there any quicker/easier way to achieve this?

share|improve this question
add comment

4 Answers 4

up vote 6 down vote accepted

There are different regular expression dialects; some (e.g. Perl's) do not require backslashes in the quantification modifier (\d{2}), some (e.g. sed) require two (\d\{2\}), and in Vim, only the opening curly needs it (\d\{2}). That's the sad state of incompatible regular expression dialects.

Also note that for matching exact numbers, you have to anchor the match so that \d\{2} won't match to digits (12) in 123. This can be done with negative look-behind and look-ahead:

\d\@<!\d\{2}\d\@!
share|improve this answer
    
Perfect. Can you please one liner info on the regex? Would be really helpful. Thanks –  mtk Sep 12 '13 at 9:08
1  
For someone struggling with the basic regexp syntax, look-behind/ahead is pretty advanced. I probably cannot describe it better than the built-in :help /\@<! and :help /\@!. Don't worry if you don't immediately understand everything; as I said, this is pretty advanced. –  Ingo Karkat Sep 12 '13 at 9:42
    
You can also put \v at the beginning of the regex if you wish to avoid having to escape the {, see :help magic for more information (it makes vim regex behave a bit more like perl regex, though there are still differences). –  evilsoup Sep 12 '13 at 12:35
    
Thank god for them vim gurus. –  Aki Sep 12 '13 at 14:43
add comment

Escaping brackets works: \d\{2\}

share|improve this answer
add comment

Try the following:

\d\{2}

and you should use \ not /

You can find out more about the regular exression of vim on this site or in vim with :help regular.

share|improve this answer
add comment

Not as pretty, but this worked for me for 5 digits in a log file.

/\<\%(\d\d\d\d\d\)\>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.