【问题标题】:Notepad++ Regular Expression Condition ReplacementNotepad++正则表达式条件替换
【发布时间】:2012-01-25 19:34:23
【问题描述】:
我有一组想要更改架构的 SQL 脚本。
create table Service.Table1 (col1 varchar(100));
create table Operation.Table2 (col1 varchar(100));
create table Support.Table3 (col1 varchar(100));
但是,架构会改变
Service -> Sev
Operation -> Opn
Support -> Spt
搜索正则表达式很简单([A-Za-z0-9_]+)\.([A-Za-z0-9_]+)
但是,如果可以的话,如何在 Notepad++ 或其他工具中进行条件替换?
谢谢!
【问题讨论】:
标签:
regex
conditional
notepad++
replace
【解决方案1】:
如果您有一组预定义的模式,您可以像这样在 Notepad++ 中使用条件替换:
查找:(?:(?<a>Service)|(?<b>Operation)|(?<c>Support))\.(?<n>[A-Z0-9_]+)
替换:(?{a}Sev:(?{b}Opn:Spt)).$+{n}
Match Case必须打勾off,正则表达式必须on。
【解决方案2】:
我会运行 replace 3 次,每个模式名称一次:
Find:
create table Service\.
Replace with:
create table Svc.
Find:
create table Support\.
Replace with:
create table Spt.
Find:
create table Operation\.
Replace with:
create table Opn.
或者这里是使用组引用的:
Find:
Service(\.[^\s]+)(.*)
Replace with:
Svc\1\2
这里\1 将保存点运算符和表名,\2 保存该行的其余部分。
【解决方案3】:
Notepad++ 正则表达式实现并不是很强大;所以,
如果可以的话,还有其他工具吗?
这是一种方法:
perl -pi.back -e '%tr=(Service=>"Sev",Operation=>"Opn",Support=>"Spt");s/(?<=create table )(\w+)/$tr{$1}/e;' TheFile
您可以在哈希%tr 中添加任意数量的Original => 'Modified'。
TheFile会在处理前备份到TheFile.back。