Notepad++正则匹配


Notepad++正则表达式字符串最长不能超过69个字符

一、支持的语法

符号 含义
. 代表除换行符外的任意字符
* 代表匹配零次或者多次
+ 表示匹配一次到多次
? 其左边的字符被匹配0次或者1次
() 影响表达式匹配的顺序(类似C++的小括号会影响表达式运算顺序),并且用作表达式的分组标记(标记从1开始)如:([a-z]bc)smn\1匹配“tbcsmntbc”
{} 指定前面的字符或分组的出现次数
[] 匹配列表中任意单个字符。如:[ab]匹配“a”或“b”;[0-9]匹配任意单个数字
[^] 匹配列表之外的任意单个字符
\ 转义字符 如:要使用 “\” 本身, 则应该使用\\
\d 单个数字
| 匹配表达式左边和右边的字符串。如:ab|bc匹配“ab”或“bc”
\d 匹配一个数字字符。等价于:[0-9]
\D \d取反,匹配一个非数字字符。等价于:[^0-9]
\s 匹配任意单个空白字符:包括空格、制表符等(注:不包括换车符和换行符)。等价于:[ \t]
\S \s取反的任意单个字符。
\w 匹配包括下划线的任意单个字符。等价于:[A-Za-z0-9_]
\W \w取反的任意单个字符。等价于:[^A-Za-z0-9_]
\b 匹配单词起始处或结尾处 如:\bin匹配int,但不匹配sing
^ 其右边的表达式被匹配在行首。如:^A匹配以“A”开头的行
$ 其左边的表达式被匹配在行尾。如:e$匹配以“e”结尾的行
\t Tab制表符 注:扩展和正则表达式都支持
\r 回车符CR 注:扩展支持,正则表达式不支持
\n 换行符LF 注:扩展支持,正则表达式不支持
Note: 以换行符结尾表示是$\r\n,而不是\r\n$

二、正则表达式诀窍

^   -->  代表开头
.*  -->  相当于like '%',任意字符
$   -->  代表结尾

三、案例

3.1、匹配时间戳

#时间戳示例
2022-12-21 14:22:24.123456

#表达式
\d{
    
    4}-\d{
    
    2}-\d{
    
    2} \d{
    
    2}:\d{
    
    2}:\d{
    
    2}\.\d+

3.2、提取指定字符串

#原始字符串
<program sector_size_in_bytes="512" filename="" label="fsg" num_partition_sectors="4096">
<program sector_size_in_bytes="512" filename="" label="dsg" num_partition_sectors="4096">
<program sector_size_in_bytes="512" filename="1.txt" label="dsg" num_partition_sectors="4096">
#提取部分
filename="" label="fsg"
filename="" label="dsg"
filename="1.txt" label="dsg"
#查找目标:
.*(filename.*label\=\"[^ ]*).*
#替换为: 
\1

#不查找filename为空的字符串
也就是说不匹配""这样的字符串,但是[^]这种表达式只能匹配单独字符,如果写了字符串,也会把字符串拆开单独匹配。那么如何匹配非某字符串这种形式呢?答案是零宽负向先行断言(?!pattern) 或者零宽负向后行断言(?<!pattern)
#查找目标:
#这里注意一下,(?!"")后面要加.*,代表.*中不匹配""。
filename=(?!"").* label=[^ ]*
#替换为: 
\1

3.3、提取单词

#原始字符串
activities-activity
#提取activity
#查找目标:
(.+)-(.+)
#替换为:
\2

3.4、查找中文字符

[\x{
    
    4e00}-\x{
    
    9fa5}]*[\x{
    
    4e00}-\x{
    
    9fa5}]

四、示例

4.1、示例1:把含目标字符串及之后的字符串全部替换

把下面的字符串

123abcfg
abc
abcd

#替换成:

123hello
hello
hello

解决方案:表达式替换
查找目标:abc.*$
替换为:hello
替换前截图:
在这里插入图片描述
替换后截图:
在这里插入图片描述

4.2、示例2:

123abcfg
abc
abcd
#替换成:
123@abcfg@
@abc@
@abcd@
#表达式
(abc.*)$ 替换为:@\1@

4.3、示例3:

str[1]abc[991]
str[2]abc[992]
str[11]abc[993]
str[222]abc[996]
#替换成
god[991]
god[992]
god[993]
god[996]

#表达式
str[[0-9]+]abc\[([0-9]+)\] 替换为:god[\1]
str[[0-9]+]abc([[0-9]+]) 替换为:god\1
OR:
str\[([0-9]+)\]abc\[([0-9]+)\] 替换为:god[\2]
str([[0-9]+])abc([[0-9]+])	替换为:god\2

4.4、示例4:

#删除所有空行
step1:a. 选择正则表达式  b. 查找串:^[ \t]*$    替换串:空
step2:a. 选择扩展       b. 查找串:\r\n\r\n   替换串:\r\n    注:多次点击替换,直到没有可替换的字串

4.5、示例5:

PERMODLOG
RESERVEDETAIL
RESERVEMAIN
#替换为:
db2 "delete from PERMODLOG " 
db2 "import from ./data/PERMODLOG.ixf of ixf modified by identityignore insert into PERMODLOG "
db2 "delete from RESERVEDETAIL " 
db2 "import from ./data/RESERVEDETAIL.ixf of ixf modified by identityignore insert into RESERVEDETAIL "
db2 "delete from RESERVEMAIN " 
db2 "import from ./data/RESERVEMAIN.ixf of ixf modified by identityignore insert into RESERVEMAIN "

#表达式
(^\w+$)
#替换为
db2 \"delete from \1 \" \r\ndb2 \"import from \.\/data\/\1\.ixf of ixf modified by identityignore insert into \1 \"

4.6、示例6:

PERMODLOG
RESERVEDETAIL
RESERVEMAIN
#替换为
db2 "export to ./data/PERMODLOG.ixf of ixf select * from PERMODLOG "
db2 "export to ./data/RESERVEDETAIL.ixf of ixf select * from RESERVEDETAIL "
db2 "export to ./data/RESERVEMAIN.ixf of ixf select * from RESERVEMAIN "
#表达式
(^\w+$)
#替换为
db2 \"export to \.\/data\/\1\.ixf of ixf select \* from \1 \"

4.7、示例7:

alter table MonQryApply add constraint PK_MonQryApply primary key (orderID);
alter table sRegInfo add constraint PK_sRegInfo primary key (MachID);
#替换为
execute immediate 'alter table MonQryApply add constraint PK_MonQryApply primary key (orderID)';
execute immediate 'alter table sRegInfo add constraint PK_sRegInfo primary key (MachID)';
#表达式
(alter.*\))
#替换为
execute immediate '\1'

4.8、示例8:把select * 替换成delete , 同时在行尾加分号

select * from test_t
#替换为
delete  from test_t;
#表达式
^select \*(.+)$
#替换为:
delete \1;

4.9、示例9:每一行的前面增加一个新行,内容为go

insert into test values(1,'zhangsan');
insert into test values(2,'lisi');
insert into test values(3,'wangwu');
#替换为
go
insert into test values(1,'zhangsan');
go
insert into test values(2,'lisi');
go
insert into test values(3,'wangwu');
#表达式
^insert(.*)
#替换为
go\r\ninsert\1

4.10、示例10:每隔一行增加一个新行,内容为go

insert into test values(1,'zhangsan');
insert into test values(2,'lisi');
insert into test values(3,'wangwu');
#替换为:
insert into test values(1,'zhangsan');
go
insert into test values(2,'lisi');
go
insert into test values(3,'wangwu');
go
#表达式
^(\w+.+)$
#替换为
\1\r\ngo

4.11、示例11:去掉sql注释行内容

--插入数据1
insert into test values(1,'zhangsan');
--插入数据2
insert into test values(2,'lisi');
--插入数据3
insert into test values(3,'wangwu');
#替换为:

insert into test values(1,'zhangsan');

insert into test values(2,'lisi');

insert into test values(3,'wangwu');
#表达式
^(--.*)$
#替换为
空

4.12、示例12:(扩展模式替换)

create index IX_BUSINESSLIST_ATM_ACCOUNTNO on BUSINESSLIST_ATM (ACCOUNTNO);
create index IX_BUSINESSLIST_ATM_COREID on BUSINESSLIST_ATM (COREID);
create index IX_BUSINESSLIST_ATM_INSERDATE on BUSINESSLIST_ATM (INSERTDATE);
#替换为
create index IX_BUSINESSLIST_ATM_ACCOUNTNO on BUSINESSLIST_ATM (ACCOUNTNOASC)
 tablespace GZHINDEX
 pctfree 10
 initrans 2
 maxtrans 255
 storage
 (
 initial 64K
 next 1M
 minextents 1
 maxextents unlimited
 );

create index IX_BUSINESSLIST_ATM_COREID on BUSINESSLIST_ATM (COREIDASC)
 tablespace GZHINDEX
 pctfree 10
 initrans 2
 maxtrans 255
 storage
 (
 initial 64K
 next 1M
 minextents 1
 maxextents unlimited
 );

create index IX_BUSINESSLIST_ATM_INSERDATE on BUSINESSLIST_ATM (INSERTDATEASC)
 tablespace GZHINDEX
 pctfree 10
 initrans 2
 maxtrans 255
 storage
 (
 initial 64K
 next 1M
 minextents 1
 maxextents unlimited
 );


#表达式
);
#替换为
ASC)\r\n tablespace GZHINDEX\r\n pctfree 10\r\n initrans 2\r\n maxtrans 255\r\n storage\r\n (\r\n initial 64K\r\n next 1M\r\n minextents 1\r\n maxextents unlimited\r\n );\r\n

4.13、示例13:给单词前后加单引号

WFD545
FDS654A
FDS7887
#替换为
'WFD545'
'FDS654A'
'FDS7887'

#表达式:
^(\w+)$
#替换为
'\1'

4.14、示例14:

alter table T_BILL_TOTAL_H modify BAGNAME varchar2(80);
alter table T_BILL_TOTAL_H modify CHECKNAME varchar2(80);
alter table T_BILL_TOTAL_NET modify BAGNAME varchar2(80);
#替换为
alter table T_BILL_TOTAL_H alter BAGNAME set data type varchar(80);
alter table T_BILL_TOTAL_H alter CHECKNAME set data type varchar(80);
alter table T_BILL_TOTAL_NET alter BAGNAME set data type varchar(80);
#表达式
modify (\s*\w+\s*) varchar2\(
#替换为
alter \1 set data type varchar\(

4.15、示例15:删除只有数字的行

asdfasdf
45646545
asdfasdf
asdfasdf
54564
asdfasdf
#替换为
asdfasdf
asdfasdf
asdfasdf
asdfasdf
#表达式
^[\d]+$\r\n
#替换为空

4.16、示例16:去掉所有行中的<>(里面不能嵌套<>)

<code><span class="kwd">import</span><span class="pln"> </span><dfn><span class="typ">BaseHTTPServer</span></dfn><span class="pln">
#替换为
import BaseHTTPServer
#表达式
<[^>]*>
#替换为空

参考资料

https://blog.csdn.net/weixin_43360896/article/details/116310179

https://blog.csdn.net/u010182162/article/details/83689008

https://blog.csdn.net/gdp12315_gu/article/details/51730584

https://www.cnblogs.com/songbiao/p/12470163.html?ivk_sa=1024320u

猜你喜欢

转载自blog.csdn.net/tttzzzqqq2018/article/details/132354518
今日推荐