Quinn
Quinn

Reputation: 9506

Vim find and replace all occurrences of a regex string

I have a file with a few thousand lines, and in this file I have some regex expressions...

The regex expressions are all over the place, and recently we've changed the expression and I need to update all of them.

I need to change all instances of: [A-z -']+ with [A-z \-']+

so I tried doing :%s/[A-z -']+/[A-z \-']+/g but that replaced all occurrences of [A-z -']+ with [A-z -'[A-z -']+

Is there some other way to do this?

Upvotes: 1

Views: 79

Answers (1)

anubhava
anubhava

Reputation: 786291

You may use this substitution:

%s/\[A-z -']/[a-zA-Z '-]/g

It is wrong to use [A-z] as it will match many more characters than just [A-Za-z] and it is better to move hyphen to end position before closing ] to get the regex right.

Upvotes: 3

Related Questions