Reman
Reman

Reputation: 8109

Replace non matching characters regex

I'm trying to create a regex which does a replace of every characters which is not within a search pattern.

p.e.
Input

whatever text text and more text
test 2 12000 text and more text
text and text
text 3 text more text
1-2000 and more text

search pattern:

let @/ = '^test \zs2 \d\d\d\d\d\s.*\n\(.*\n\)\{-}text 3\ze'

(matching from just before '2' till just after '3')

I want to replace all other characters outside the matching with the character _.

expected output

________________________________
_____2 text and more text
text and text
text 3_______________
_____________

I use this regex which works fine but does not work if there are multiple lines in search pattern:

exe "%s/\\(@/\\)\\zs\\|./\\=submatch(1)!=''?'':'_'/g"   

How can I make it work also with multiple lines in search pattern?

Upvotes: 0

Views: 345

Answers (2)

Caek
Caek

Reputation: 592

Modyfing alpha bravo's solution and adapting to vim:

:%s/\(2\_.*3\)*[^\r\n]/\1_/g

Should do the trick.

The key here is \_. which matches any character including newlines.
Which is the equivalent to /s in PCRE

Upvotes: 2

alpha bravo
alpha bravo

Reputation: 7948

Use this pattern (2.*3)*[^\r\n] with gs options and replace with $1_
Demo

Upvotes: 1

Related Questions