1

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that worked - Could you explain why '- will match hyphens but -' doesn't?
hyphen, if used in the middle of a character class is used as a range rather than literally. So your regex will match characters from space (0x20) to ' (0x27)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.