Search code examples
regexnotepad++google-sheets-api

Delete everything except a word


I have hours searching for a way in regex to delete everything in a line except a certain text. It will be something like this:

Before:

Caracas
UNUSABLE TEXT Caracas UNUSABLE TEXT 
Caracas

After:

Caracas
Caracas
Caracas
Caracas

I have make so many tests with this:

Find: .(text_you_need_to_keep).

Replace: $1

But I can't get it to work.


Solution

  • You can use

    Find What:      (Caracas)|\R*(?:(?!Caracas).)+\R*
    Replace With: (?{1}$1:\nCaracas\n)

    Details:

    • (Caracas) - Group 1: Caracas
    • | - or
    • \R* - zero or more line breaks
    • (?:(?!Caracas).)+ - one or more chars other than line break chars (as many as possible) that does not start a Caracas char sequence
    • \R* - zero or more line breaks

    If the Group 1 matches, the replacement is just this Group 1 value, else, the replacement is a Caracas with newline chars on both ends.

    See the regex demo:

    enter image description here