regex 提取组件并添加到字符串

mspsb9vt  于 2023-08-08  发布在  其他
关注(0)|答案(3)|浏览(88)

我有很多这种格式的insert语句:

INSERT INTO MY_TABLE (MY_ID, MY_NAME) VALUES(l_some_variable,'SOME NAME');

字符串
我需要改变它,使它们采用这种形式:

INSERT INTO MY_TABLE (MY_ID, MY_NAME) VALUES(l_some_variable, 'SOME NAME') RETURNING MY_ID INTO t_capital('l_some_variable ---- SOME NAME');


基本上需要在结尾处加上:第一个月
有成千上万的语句,使这非常繁琐,如果手动完成。我对正则表达式很不在行,在notepad++中也没能弄清楚。

ffscu2ro

ffscu2ro1#

  • Ctrl+H
  • 查找:INSERT INTO MY_TABLE \(MY_ID, MY_NAME\) VALUES\((\w+),'([^']+)'\)\K(?=;)
  • 替换为:RETURNING MY_ID INTO t_capital\('$1 ---- $2'\)
    ***滴答 * 缠绕 *
    ***SELECT * 正则表达式 *
    *取消勾选. matches newline
  • 全部更换
    说明:
INSERT INTO MY_TABLE \(MY_ID, MY_NAME\) VALUES\(
(\w+)           # group 1, 1 or more word character
,'              # literally
([^']+)         # group 2, 1 or more any character that is not single quote
'\)             # literally
\K              # forget all we have seen until this position
(?=;)           # positive lookahead, make sure we have a semicolon after

字符串

替换:

RETURNING MY_ID INTO t_capital\('$1 ---- $2'\)      # in Notepad++, parenthesis hav2 to be escaped in replacement part

前截图:


的数据

后截图:


xzabzqsa

xzabzqsa2#

您可以尝试在正则表达式模式下执行以下查找和替换:

Find:    ^INSERT INTO (\w+) \((\w+)(.*VALUES.*);
Replace: INSERT INTO $1 ($2$3 RETURNING $2 INTO t_capital('l_some_variable ---- SOME NAME');

字符串

Demo

wsxa1bj1

wsxa1bj13#

Viable patterns might look similar to ...

/^INSERT\s+INTO\s+(?<table>.*?)\s+\((?<id>.*?),\s*(?<name>.*?)\)\s+VALUES\((?<value1>.*?),\s*'(?<value2>.*?)'\);$/gm;

字符串
正则表达式是PCRE(2)和ECMAScript兼容的。一个使用上述模式的JavaScript替换示例可能看起来像这样...

const regXCapture =
  // see ... [https://regex101.com/r/UAyDs5/1]
  /^INSERT\s+INTO\s+(?<table>.*?)\s+\((?<id>.*?),\s*(?<name>.*?)\)\s+VALUES\((?<value1>.*?),\s*'(?<value2>.*?)'\);$/gm;

const sampleData =
`INSERT INTO FOO (barId, bazName) VALUES(bizVal, 'buzzVal');
INSERT INTO BAR (bazId, bizName) VALUES(buzzVal, 'boozVal');`

console.log(
  sampleData.replace(
    regXCapture,
    "INSERT INTO $1 ($2, $3) VALUES($4, '$5') RETURNING $2 INTO t_capital('$4 ---- $5');"
  )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

的数据

相关问题