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
  • 5,354
  • 5
  • 27
  • 32
Alexander
  • 65
  • 1
  • 6

2 Answers2

0

You may try using -

((UNION[^;]*);)

Here's a link demonstrating how it works.

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

Oshan
  • 168
  • 13
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
  • 6,349
  • 1
  • 3
  • 18