Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I can't just delete all of the hyphens, because elsewhere they are used as minus signs. I can find these dates with [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} but I can't figure out how to keep those three different digit wildcards in memory. My attempts end up replacing the dates with the search string (e.g., :%s/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}/[0-9]\{4\}[0-9]\{2\}[0-9]\{2\}/g).

Any ideas? If there's a command line solution, I'd love to learn that, too. Thanks!

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You just need to use capture groups:

:%s/\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\1\2\3/g

The parentheses capture the matches within, then the backreferences output those capture groups. Since the hyphens are outside the groups, they're excluded.

share|improve this answer
1  
You can also simplify [0-9] with \d –  Luc Hermitte Feb 14 '11 at 12:54
add comment

I don't know vim so this is a workaround that you can still perform in vim:

Since you can find the hyphens that occur in dates, replace them with something else (a character or sttring) that you know doesn't occur in the document. Then do a second search/replace to replace your new character/string with nothing, thereby deleting it.

share|improve this answer
    
but wouldn't that delete the minus signs I need elsewhere? –  Richard Herron Feb 13 '11 at 5:03
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.