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文章來源:http://www.zghlxwxcb.cn/news/detail-672030.html
https://www.cnblogs.com/songbiao/p/12470163.html?ivk_sa=1024320u文章來源地址http://www.zghlxwxcb.cn/news/detail-672030.html
到了這里,關于Notepad++正則匹配的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!