ajwood
ajwood

Reputation: 19057

Regex Searching in vim

I'm using vim to do some pattern matching on a text file. I've enabled search highlighting so that I know exactly what is getting matched on each search and am getting confused.

Consider searching for [a-z]* on the following text:

123456789abcdefghijklmnopqrstuvwxyxz987654321ABCDEFGHIJKLMNOPQRSTUVWQXZ

I expected this search to match zero or more consecutive characters that are in the range [a-z]. Instead, I get a match on the entire line.

Should this be the expected behaviour?

Thanks,

Andrew

Upvotes: 3

Views: 249

Answers (3)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799420

You're not getting a match on the entire line, you're getting a match on every character. Your pattern also matches nothing at all, which is matched by every single character.

Upvotes: 2

Donald Miner
Donald Miner

Reputation: 39943

Empty string matches [a-z]*... therefore this thing is matching everywhere. Perhaps you want to cut down some of the cases by doing [a-z]+ (1 or more), or [a-z]{4,} (4 or more).

Upvotes: 2

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143314

It's matching the empty strings that occur after every character. It has no way of highlighting empty ranges, so it looks like everything is highlighted.

Try searching for [a-z]\+ instead.

Upvotes: 7

Related Questions