Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

1,$s/\v\/\*\!\d+\s*([^*]+)\*\//\1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top