Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace all text except "pattern" using regex

I have a piece of software that, given a regex, can perform a find and replace. It's not a coding language, so no suggestions other than regex will be useful. I have searched looking for an expression that will help me find all text except "pattern". The pattern is not necessarily at the start or the end of the string, and can occur multiple times. Given the string:

red blue yellow green orange purple blue black green white

and a pattern:

blue.*?green

a pattern that appears twice on the line, I am trying to select everything but. (with a view to removing the selected and leaving just the pattern). I have tried:

FIND - ".*?(blue.?green)" 
REPLACE - "$1", 

but that still leaves "white" on the end. Any help would be appreciated.

like image 758
Gus Sharples Avatar asked Feb 17 '26 18:02

Gus Sharples


1 Answers

It's usually something like this

Find (?:(?!blue.*?green).)*(blue.*?green)?
Replace $1

Expanded

 (?:
      (?! blue .*? green )
      . 
 )*
 ( blue .*? green )?           # (1)

Output

blue yellow greenblue black green

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!