0

I want to comment piece of pretty similar text in document in Sublime using replace with regex.

Input:

@b =
    SELECT      *
    FROM        @e;
    UNION ALL
    SELECT      *
    FROM        @c_historical;


@r =
    SELECT      *
    FROM        @f;
    UNION ALL
    SELECT      *
    FROM        @y_historical;

Expected output:

@b =
    SELECT      *
    FROM        @e;
    /*UNION ALL
    SELECT      *
    FROM        @c_historical;*/


@r =
    SELECT      *
    FROM        @f;
    /*UNION ALL
    SELECT      *
    FROM        @y_historical;*/

I tried to use this regex (UNION ALL\D*historical;) and replace with /*\1*/ (using Sublime) but getting another result

@b =
    SELECT      *
    FROM        @e;
    /*UNION ALL
    SELECT      *
    FROM        @c_historical;


@r =
    SELECT      *
    FROM        @f;
    UNION ALL
    SELECT      *
    FROM        @y_historical;*/

How I could kind of separate this 1 match to 2 different?

Biffen
  • 6,249
  • 6
  • 28
  • 36
Alexander
  • 331
  • 1
  • 2
  • 11

2 Answers2

0

You may try using -

((UNION[^;]*);)

Here's a link demonstrating how it works.

https://regex101.com/r/kpiVV8/1

Oshan
  • 176
  • 15
0

Your RegEx is pretty close. Just change it to a non-greedy quantifier by replacing * with *? will do the job:

(UNION ALL\D*?historical;)

RegEx Demo

The non-greedy version will give the shortest match whenever possible. While your original greedy quantifier * matches as many as possible (longest match). See here for a reference.

SeaBean
  • 22,547
  • 3
  • 13
  • 25