Notepad++ regular matching


Notepad++ regular expression string cannot exceed 69 characters

1. Supported syntax

symbol meaning
. Represents any character except newline
* means match zero or more times
+ means match one to many times
? The character to its left is matched 0 or 1 times
() Affects the order of expression matching (parentheses similar to C++ will affect the order of expression operations), and is used as a grouping mark for expressions (marks start from 1) such as: ([az]bc)smn\1 matches "tbcsmntbc"
{} Specifies the number of occurrences of the preceding character or group
[] Matches any single character in the list. Such as: [ab] matches "a" or "b"; [0-9] matches any single number
[^] matches any single character not in the list
\ Escape characters such as: To use "\" itself, you should use\\
\d single digit
| Matches the string on the left and right of the expression. Such as: ab|bc matches "ab" or "bc"
\d Matches a numeric character. Equivalent to: [0-9]
\D \d Negates, matches a non-digit character. Equivalent to:[^0-9]
\s Match any single whitespace character: including spaces, tabs, etc. (Note: Carriage and newline characters are not included). Equivalent to: [ \t]
\S \s negates any single character.
\w Match any single character including underscore. Equivalent to: [A-Za-z0-9_]
\W \w negates any single character. Equivalent to:[^A-Za-z0-9_]
\b Match the beginning or end of a word such as: \bin matches int, but does not match sing
^ The expression to its right is matched at the beginning of the line. For example: ^A matches lines beginning with "A"
$ The expression to its left is matched at the end of the line. For example: e$ matches lines ending with "e"
\t Tab Note: Both extensions and regular expressions are supported
\r Carriage return character CR Note: extended support, regular expressions do not support
\n Line break LF Note: extended support, regular expressions do not support
Note: 以换行符结尾表示是$\r\n,而不是\r\n$

Two, regular expression tricks

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

3. Case

3.1. Matching timestamp

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

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

3.2. Extract the specified string

#原始字符串
<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. Extracting words

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

3.4. Find Chinese characters

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

4. Examples

4.1. Example 1: Replace all strings including the target string and after

put the following string

123abcfg
abc
abcd

#Replace with:

123hello
hello
hello

Solution: Expression replacement
Find target: abc.*$
Replace with: hello
Screenshot before replacement:
insert picture description here
Screenshot after replacement:
insert image description here

4.2. Example 2:

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

4.3. Example 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. Example 4:

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

4.5. Example 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. Example 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. Example 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, Example 8: replace select * with delete, and add a semicolon at the end of the line

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

4.9, Example 9: add a new line in front of each line, the content is 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, Example 10: add a new line every other line, the content is 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. Example 11: Remove the sql comment line content

--插入数据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. Example 12: (extended pattern replacement)

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. Example 13: Add single quotation marks before and after words

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

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

4.14. Example 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. Example 15: Delete rows with only numbers

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

4.16. Example 16: Remove <> in all lines (<> cannot be nested inside)

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

References

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

Guess you like

Origin blog.csdn.net/tttzzzqqq2018/article/details/132354518