doobop
doobop

Reputation: 4510

Vim multiline search with regex

In Vim, I'm trying to find 2 consecutive lines that have a '9' in the 31st column. I've tried

^.\{30\}9.*$.\{30\}9
^.\{30\}9.*\n.\{30\}9
^.\{30\}9\_.*^.\{30\}9

With no luck. How do I specify the new line character in the regular expression? I'd like to use the / operator and not s: or g:

Sample data:

1234567  300.0000000 2223456 390-9.00000000000000D+02 1.00000D-06 111  5.2 900.0 95.6
1234567  300.0000000 2723456 10  2.00000000000000D+04 7.83912D-06 111  6.2 900.0 95.6
1234567  300.0000000 2723456 190-3.00000000000000D+03 1.00000D-06 111  7.2 900.0 95.6
1234567  300.0000000 2823456120  2.00000000000000D+04 5.13183D-05 111  8.2 900.0 95.6
1234567  300.0000000 28234561290-1.00000000000000D+03 1.00000D-06 111  9.2 900.0 95.6
1234567  300.0000000 2723456 190-3.00000000000000D+03 1.00000D-06 111  7.2 900.0 95.6
1234567  300.0000000 2823456120  2.00000000000000D+04 5.13183D-05 111  8.2 900.0 95.6

Lines 5 & 6 should be the one match.

Adding Solaris tag to see if anyone may know that flavor of vi.

Upvotes: 0

Views: 364

Answers (1)

Adam Katz
Adam Katz

Reputation: 16118

Just in case your file has DOS line endings (\r\n), one of these should work:

 ^.\{30\}\zs9.*\r\?\n.\{30\}9
 ^.\{30\}\zs9.*[\r\n]\{1,2\}.\{30\}9

I added an optional carriage return character to account for DOS line endings. I also added \zs to limit the match to begin at the first 9 (this will place your cursor on that character when you search for it, but it'll be more dramatic if you use set hlsearch). You don't need the \zs for the match.

My first vim regex above is equivalent to ^.{30}\K9.*\r?\n.{30}9 in PCRE, as explained here.

The second regex is a little broader, allowing any combination of the two line break characters but also allowing a blank line (\n\n) between two entries, which may or may not be acceptable to you.

If neither of these work for you, I recommend you specify your versions of Vim and Solaris in your question and perhaps add the tag to the question.

Upvotes: 1

Related Questions