How do I get Vim to match a line not beginning with a seven-digit number?

Viewed 24000

I have a file with about 1000 lines. All lines begin with a seven-digit number except for the occasional line. I need to catch these lines and actually join them with the previous line.

I've managed to be able to match any line that begins with a seven-digit number by using the following regex pattern:

^\d\{7}

I can't seem to get it to match any line that does not match this pattern though, which is really what I'm after.

As a second question that I'll embed into this one. Is it possible to have any lines that match (or do not match to stay consistent with what I'm trying to do) to join themselves to the previous line (as opposed to the J command that brings the next line up to the current one)?

Thanks

4 Answers

Now, for the real answer

The regular expression which matches strings that do not begin with 7 digits is very simple:

.{0,6}([^0-9].*)?

A more classic regex equivalent without {} syntax is actually more readable: it visually shows us what is going on:

(|.|..|...|....|.....|......)([^0-9].*)?

I.e. match between 0 and 6 characters which can be anything, optionally followed by a nondigit that, if it occurs, may be followed by zero or more additional characters. This ensures that if a string seven characters characters or longer is matched, at least one of the first seven characters is a nondigit.

To translate this to matching lines in Vim, we add some escaping and anchoring:

^.\{0,6\}\([^0-9].*\)\?$

I don't "do" \d; it's too new-fashioned. :)

Related