[英]Regex to match anything, string, multiple,strings including newline non greedy in VIM
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: 我使用vim replace将此正则表达式替换为大写:
:%s/select\\_.*\\from\\_.*\\where/\\U&/gic
The regex select\\_.*\\from\\_.*\\where
is not good when there are other select queries: 当存在其他选择查询时,正则表达式select\\_.*\\from\\_.*\\where
不好:
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
There is non-greedy mode in vim regex. Vim里正则表达式非贪婪模式。 Instead of use *
, use \\{-}
. 代替使用*
,使用\\{-}
。
The regex select\\_.\\{-}from\\_.\\{-}where
, as I think, is what you want. 我想,正则表达式select\\_.\\{-}from\\_.\\{-}where
。
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\}.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.