How can I use regex to replace a string on multiple lines, but stop when a specific word is reached?

52 Views Asked by At

For example:

Hello, my name is Bob
Hello, my name is Mary
Hello, my name is Steve
END

How can I replace "Hello, my name is ", but stop at the word END?

1

There are 1 best solutions below

0
On

You can use a positive lookahead to make sure END is followed after your pattern using this regex:

Hello, my name is(?=[\s\S]*?\bEND\b)

RegEx Demo