我想根據一些規則在 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沒有成功-它什么都不匹配。
我正在尋找一種在盡可能少的替換步驟中滿足規則的解決方案。
uj5u.com熱心網友回復:
將單引號替換為 2 個引號后,您可以一步完成剩下的作業:
處理多個 TAB 不是很優雅,但它可以作業。
- Ctrl H
- 找什么:
^(?:(\t)(\t)?(\t)?(\t)?(\t)?(\S.*)|\h*|(. ))$ - 用。。。來代替:
@lin(?1 @tab (?2@tab )(?3@tab )(?4@tab )(?5@tab )'$6')(?7 '$7') - 檢查 火柴盒
- 檢查 環繞
- CHECK 正則運算式
- 取消選中
. matches newline - Replace all
解釋:
^ # beginning of line
(?: # non capture group
(\t) # group 1, tabulation
(\t)? # group 2, tabulation, optional
(\t)? # group 3, tabulation, optional
(\t)? # group 4, tabulation, optional
(\t)? # group 5, tabulation, optional
(\S.*) # group 6, a non-space character followed by 0 or more any character but newline
| # OR
\h* # 0 or more horizontal spaces
| # OR
(. ) # group 7, 1 or more any character but newline
) # end group
$ # end of line
替換:
@lin # literally
(?1 # if group 1 exists
@tab # add this
(?2@tab ) # if group 2 exists, add a second @tab
(?3@tab ) # id
(?4@tab ) # id
(?5@tab ) # id
'$6' # content of group 6 with single quotes
) # endif
(?7 # if group 7 exists
# plus sign
'$7' # content of group 3 with single quotes
) # endif
截圖(之前):

截圖(之后):

uj5u.com熱心網友回復:
您可以在這里使用三個替換,因為您需要在相同的位置替換,所以不太可能(沒有額外的假設)減少這里的步驟數。
第 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 步:將每個 TAB 替換 @lin 為@tab :
查找內容:(\G(?!^)|^\ @lin\ )\t
替換為:$1@tab
詳情:
(\G(?!^)|^\ @lin\ )- 第 1 組:任一個\G(?!^)- 上一場比賽結束|- 要么^\ @lin\- 行首和@lin字串
\t- 一個 TAB 字符。
替換是第 1 組值和@tab 字串的串聯。
請參閱此正則運算式在線演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/432910.html
上一篇:使用grep過濾的檔案串列,將該檔案串列轉發到grep進一步
下一篇:如何用其他字串的參考拆分字串串列
