正则表达式生成动态sql
我想根据一些规则在Notepad++上生成动态sql。这些规则包括所有内容,因此不需要 sql 知识,如下所示:
- 动态 sql 必须将每个单引号用另一个单引号转义('hello' 变为 ''hello'')
- 每行应以 "+@lin" 开头
- 如果一行只有空格,则“+@lin”后面不应该有任何内容,尽管遵循以下规则
- 将“+@lin”后面的每个 \t 直接替换为“+@tab”
- 在 @lin/@tab 之后添加“ +'”序列
- 在行尾添加单引号
因此,作为一个例子,这个输入:
select 1,'hello'
from --two tabs exist after from
table1
应该变成:
+@lin+'select 1,''hello'''
+@lin+'from --two tabs exist after from'
+@lin
+@lin+@tab+'table1'
我现在有以下 4 个步骤:
- 用双引号替换单引号以覆盖规则 1
- 替换
^(\t*)(.*)$ 替换为
\+@lin\1\+'\2'
以覆盖规则 2,5,6 - 将
\t
替换为\+@tab 覆盖规则 4
- 替换
(\+@tab)*\+''$
没有任何内容可以覆盖规则 3
请注意,这大部分都有效,除了第三个替换,它替换所有选项卡,而不仅仅是开头的选项卡。我尝试了 (?<=^\t*)\t
但没有成功 - 它没有匹配任何内容。
我正在寻找一种以尽可能少的更换步骤满足规则的解决方案。
I want to generate dynamic sql on Notepad++ based on some rules. These rules include everything, so no sql knowledge is needed, and are the following:
- Dynamic sql must have each single quote escaped by another single quote ( 'hello' becomes ''hello'')
- Each line should begin with "+@lin"
- If a line has only whitespace, nothing should be following the "+@lin", despite following rules
- Replace each \t directly following "+@lin" with "+@tab"
- Add " +' " after the @lin/@tab sequence
- Add a single quote at the end of line
So, as an example, this input:
select 1,'hello'
from --two tabs exist after from
table1
should become:
+@lin+'select 1,''hello'''
+@lin+'from --two tabs exist after from'
+@lin
+@lin+@tab+'table1'
What I have for now is the following 4 steps:
- Replace single quote with double quotes to cover rule 1
- Replace
^(\t*)(.*)$
with\+@lin\1\+'\2'
to cover rules 2,5,6 - Replace
\t
with\+@tab
to cover rule 4 - Replace
(\+@tab)*\+''$
with nothing to cover rule 3
Notice that this mostly works, except for the third replacement, which replaces all tabs, and not only the ones at the beginning. I tried (?<=^\t*)\t
with no success- it matches nothing.
I'm looking for a solution which satisfies the rules in as few replacement steps as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将单引号替换为 2 个引号后,您可以一步完成其余操作:
对于处理多个 TAB 来说不是很优雅,但它可以工作。
^(?:(\t)(\t)?(\t)?(\t)?(\t) ?(\S.*)|\h*|(.+))$
+@lin(?1+@tab+(?2@tab+)(?3@tab+)(?4@tab+)(?5@tab+)'$6')(?7+'$7')
说明:
替换:
屏幕截图(之前):
屏幕截图(之后):
After replacing single quotes with 2 quotes, you can do the rest in a single step:
Not very elegant for processing multiple TABs, but it works.
^(?:(\t)(\t)?(\t)?(\t)?(\t)?(\S.*)|\h*|(.+))$
+@lin(?1+@tab+(?2@tab+)(?3@tab+)(?4@tab+)(?5@tab+)'$6')(?7+'$7')
. matches newline
Explanation:
Replacement:
Screenshot (before):
Screenshot (after):
您可以在这里使用三个替换,不太可能(没有额外的假设)减少这里的步骤数,因为您需要在相同的位置进行替换。
第 1 步:将单引号替换为双引号 -
'
替换为''
。到目前为止还没有正则表达式,但您可以打开正则表达式复选框。第 2 步:在行首添加
+@lin+
,如果有非空白字符,则仅用'
包裹其内容该行(同时保留第一个'
之前的所有 TAB):查找内容:
^(\t*+)(\h*\S)?+( .*)
替换为:
+@lin+$1(?2'$2$3':)
详细信息:
^
- 开头一行(\t*+)
- 第 1 组 ($1
):零个或多个 TAB(\h*\S)?+
-第 2 组 ($2
):由任何零个或多个水平空白字符组成的可选序列,然后是一个非空白字符(.*)
- 第 3 组 ($3
):该行的其余部分+@lin+$1(?2'$2$3':)
- 将匹配替换为+@lin+
+ 第 1 组值(即找到选项卡),然后 - 仅当第 2 组匹配 -'
+ 第 2 组 + 第 3 组值 +'
第 3 步:将
+@lin+
后的每个 TAB 替换为 < code>@tab+:查找内容:
(\G(?!^)|^\+@lin\+)\t
替换为:
$1@tab+
详细信息:
(\G(?!^)|^\+@lin\+)< /code> - 第 1 组:任一
\G(?!^)
- 上一场比赛结束|
- 或^\+@lin\+
- 行的开头和+@lin+
字符串\t
- TAB 字符。替换是组 1 值和
@tab+
字符串的串联。请参阅此正则表达式在线演示。
You can use three substitutions here, it is not quite possible (without additional assumptions) to reduce the number of steps here since you need to replace at the same positions.
Step 1: Replace single quotes with double -
'
with''
. No regex so far, but you can have the regex checkbox on.Step 2: Add
+@lin+
at the start of the line and only wrap its contents with'
if there is any non-whitespace char on the line (while keeping all TABs before the first'
):Find What:
^(\t*+)(\h*\S)?+(.*)
Replace With:
+@lin+$1(?2'$2$3':)
Details:
^
- start of a line(\t*+)
- Group 1 ($1
): zero or more TABs(\h*\S)?+
- Group 2 ($2
): an optional sequence of any zero or more horizontal whitespace chars and then a non-whitespace char(.*)
- Group 3 ($3
): the rest of the line+@lin+$1(?2'$2$3':)
- replaces the match with+@lin+
+ Group 1 value (i.e. tabs found), and then - only if Group 2 matches -'
+ Group 2 + Group 3 values +'
Step 3: Replace each TAB after
+@lin+
with@tab+
:Find What:
(\G(?!^)|^\+@lin\+)\t
Replace With:
$1@tab+
Details:
(\G(?!^)|^\+@lin\+)
- Group 1: either\G(?!^)
- end of the previous match|
- or^\+@lin\+
- start of a line and+@lin+
string\t
- a TAB char.The replacement is the concatenation of Group 1 value and
@tab+
string.See this regex online demo.