1

I want to replace lowercase strings within:

SELECT lower1, lower2, lower3 FROM lower4, lower5 WHERE

I use vim replace to replace them to upper case with this regex:

:%s/select\_.*\from\_.*\where/\U&/gic

The regex select\_.*\from\_.*\where is not good when there are other select queries:

for example it selects everything in this query and affects the strings that cannot be uppercased

SELECT lower1, lower2, lower3 FROM lower4, lower5 WHERE lower1=cannot_be_uppercased
UNION all
SELECT lower1, lower2, lower3 FROM lower6, lower7 WHERE lower1=cannot_be_uppercased
shift66
  • 11,760
  • 13
  • 50
  • 83
bbnn
  • 3,505
  • 10
  • 50
  • 68

1 Answers1

1

There is non-greedy mode in vim regex. Instead of use *, use \{-}.

The regex select\_.\{-}from\_.\{-}where , as I think, is what you want.

Here is some docs.

:h non-greedy

                            *non-greedy*
If a "-" appears immediately after the "{", then a shortest match
first algorithm is used (see example below).  In particular, "\{-}" is
the same as "*" but uses the shortest match first algorithm.  BUT: A
match that starts earlier is preferred over a shorter match: "a\{-}b"
matches "aaab" in "xaaab".

Example         matches ~
ab\{2,3}c       "abbc" or "abbbc"
a\{5}           "aaaaa"
ab\{2,}c        "abbc", "abbbc", "abbbbc", etc.
ab\{,3}c        "ac", "abc", "abbc" or "abbbc"
a[bc]\{3}d      "abbbd", "abbcd", "acbcd", "acccd", etc.
a\(bc\)\{1,2}d      "abcd" or "abcbcd"
a[bc]\{-}[cd]       "abc" in "abcd"
a[bc]*[cd]      "abcd" in "abcd"

The } may optionally be preceded with a backslash: \{n,m\}.
Ade YU
  • 2,292
  • 3
  • 18
  • 28