Using VI how can I search and replace the following statement?

I need to replace the following:

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

      

to

CREATE TABLE IF NOT EXISTS `access`

      

I tried

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

      

But it didn't seem to work. What am I doing wrong?

0


source to share


3 answers


vim requires dumping back + (or use * instead). Also, you need a backslash bracket in vim. Thus:

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

      



Yes, vim old-style posix regexes suck: /

Edit: As mentioned in the comments below, + works if selected as \ +. And \ d is indeed supported, oops. Edited the regex example to fix this. Also see Brian Carper's example for a more concise and readable version.

+4


source


Use "very magic" and use non-default delimiters to make it easier to read (and remember).

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

      



Without "very magic", you need to put a backslash before +

and ()

(but not before *

or some other things). He's not very consistent.

+4


source


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

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

      

+1


source







All Articles