tags:

views:

210

answers:

3

I need to replace the following:

CREATE TABLE /*!32312 IF NOT EXISTS*/ `access`

to

CREATE TABLE IF NOT EXISTS `access`

I've tried

:%s/\/\*\!\d+(.*)\*\//\1/g

But that didn't seem to go. What am I doing wrong?

+3  A: 

vim requires backslashing + (or use * instead). Also, you need to backslash grouping parenthesis in vim. Thus:

:%s/\/\*\!\d\+\(.*\)\*\//\1/g

Yes, vim's old-style posix regexes suck :/

Edit: As mentioned in the comments below, + does work if escaped as \+. And \d actually is supported, oops. Edited the example regex to correct for this. Also see Brian Carper's example for a more succinct and readable version.

bdonlan
vim does support +, you just have to escape it, "\+"
Chad Birch
Apparently it does. Awesome. I still want a PCRE-for-vim plugin though :)
bdonlan
Just deleted my answer: it turns out I do not know enough about the regex support specifics in Vim ;)
VonC
You might as well edit the answer to make it up-vote-able and accept-able
Nathan Fellman
@Nathan This answer works just fine, but the extra comments are also great to have. Thanks all.
altCognito
\d does work in Vim and means [0-9]. See :h /\d.
Brian Carper
u can use + instead of \+ using \v (very magic) option. check :h \v in vim
chappar
+4  A: 

Use "very magic", and use delimiters other than the default to make this easier to read (and remember).

:%s@\v/\*!\d+(.*)\*/@\1@g

Without "very magic" you have to put a backslash in front of + and () (but not in front of * or some other things). It's not very consistent.

Brian Carper
+1  A: 

Slightly different and more efficient with [^*]+ :-)

1,$s/\v\/\*\!\d+\s*([^*]+)\*\//\1
chappar