Home  >  Article  >  Development Tools  >  About notepad++ regular expression replacement string

About notepad++ regular expression replacement string

藏色散人
藏色散人forward
2020-12-02 15:02:205432browse

The following tutorial column of notepad will introduce you to the method of replacing strings with regular expressions in notepad. I hope it will be helpful to friends in need!

About notepad++ regular expression replacement string

The expression is a query string, which contains general characters and some special characters. Special characters can expand the ability to find strings. Regular expressions are used in The role of finding and replacing strings cannot be ignored, it can greatly improve work efficiency.

EditPlus's search, replace, and search in files support the following regular expressions:

表达式 说明 
 /t 制表符. 
 /n 新行. 
 . 匹配任意字符. 
 | 匹配表达式左边和右边的字符. 例如, "ab|bc" 匹配 "ab" 或者 "bc". 
 [] 匹配列表之中的任何单个字符. 例如, "[ab]" 匹配 "a" 或者 "b". "[0-9]" 匹配任意数字. 
 [^] 匹配列表之外的任何单个字符. 例如, "[^ab]" 匹配 "a" 和 "b" 以外的字符. "[^0-9]" 匹配任意非数字字符. 
 * 其左边的字符被匹配任意次(0次,或者多次). 例如 "be*" 匹配 "b", "be" 或者 "bee". 
 + 其左边的字符被匹配至少一次(1次,或者多次). 例如 "be+" 匹配 "be" 或者 "bee" 但是不匹配 "b". 
 ? 其左边的字符被匹配0次或者1次. 例如 "be?" 匹配 "b" 或者 "be" 但是不匹配 "bee". 
 ^ 其右边的表达式被匹配在一行的开始. 例如 "^A" 仅仅匹配以 "A" 开头的行. 
 $ 其左边的表达式被匹配在一行的结尾. 例如 "e$" 仅仅匹配以 "e" 结尾的行. 
 () 影响表达式匹配的顺序,并且用作表达式的分组标记. 
 / 转义字符. 如果你要使用 "/" 本身, 则应该使用 "//".

Example:

原始串 
str[1]abc[991]; 
str[2]abc[992]; 
str[11]abc[993]; 
str[22]abc[994]; 
str[111]abc[995]; 
str[222]abc[996]; 
str[1111]abc[997]; 
str[2222]abc[999];
目标串: 
 abc[1]; 
 abc[2]; 
 abc[11]; 
 abc[22]; 
 abc[111]; 
 abc[222]; 
 abc[1111]; 
 abc[2222];

Processing:
Search string: str /[([0-9] )/]abc/[[0-9] /]
Replacement string: abc[/1]

1. Contains The line of “hello word”

^.*hello word.*$

2. The line starting with “hello word”

^hello word.*$

3. Lines ending with "hello word"

.*hello word$

## is replaced with:

Find target\n([a-zA-Z0-9])

or\r([a-zA-Z0-9])

Replace Target $1 (the newline in front of http becomes ',' , and then add others)

【1】Regular expression application - replace the specified content to the end of the line
The original text is as follows two lines
abc aaaaa
123 abc 444
I hope that every time I encounter "abc", Then replace "abc" and the content following it to the end of the line as "abc efg"
That is, the above text is finally replaced with:
abc efg
123 abc efg
Solution:
① In In the Replace dialog box, enter "abc.*" in the search content
② Also check the "Regular Expression" checkbox, and then click the "Replace All" button
Among them, the meaning of the symbols is as follows:
" ." = Match any character
"*" = Match 0 times or more
Note: It is actually a regular expression replacement. Here is just a summary of some issues that have been raised, purely from the perspective of the regular expression itself. , thousands of special cases can be derived.
[2] Regular expression application - number replacement
I hope to replace
asdadas123asdasdas456asdasdasd789asdasd
with:
asdadas[123]asdasdas[456]asdasdasd[789]asdasd
in replacement In the dialog box, check the "Regular Expression" checkbox;
Enter "[0-9][0-9][0-9]" in the search content without quotation marks
"Replace with :" Enter "[/0/1/2]" without quotation marks
The range is the range you are operating in, and then select Replace.
In fact, this is also a special case of regular expressions. "[0-9]" means to match any special case between 0 and 9, and "[a-z]" means to match any special case between a and z.
"[0-9]" is used repeatedly above to represent three consecutive numbers
"/0" represents the prototype corresponding to the first "[0-9]", and "/1" represents the second prototype corresponding to "[0-9]", and so on
"[", "]" are simple characters, indicating adding "[" or "]", if you enter "other/0/1/2 Other", the replacement result is:
asdadas other 123 other asdasdas other 456 other asdasdasd other 789 other asdasd
Function enhancement (by jiuk2k):
If the search content "[0-9][0- 9][0-9]" is changed to "[0-9]*[0-9]", corresponding to 1 or 123 or 12345 or...
You can customize it according to your needs
There are many related contents, you can customize it yourself Refer to the syntax of regular expressions to study carefully
[3] Regular expression application - delete the specified characters at the end of each line
Because these characters also appear in the line, you must not use simple Replacement implementation
For example
12345 1265345
2345
You need to delete the "345" at the end of each line
This is also considered the usage of regular expressions. In fact, it should be relatively simple if you look carefully at the regular expressions, but Since this question has been raised, it means that there is a process of understanding regular expressions. The solution is as follows
Solution:
In the replacement dialog box, enable the "Regular Expression" checkbox
In the search content Enter "345$"
Here "$" means matching from the end of the line
If you match from the beginning of the line, you can use "^" to achieve it, but EditPlus has another function that can easily delete the characters at the beginning of the line. String
a. Select the line to be operated
b. Edit-Format-Delete line comment
c. Enter the first character of the line to be cleared in the pop-up dialog box and confirm
[4] Regular expression Formula application - replace multiple lines with half-angle brackets
Hundreds of web pages have the following piece of code:
/n
Enable the "Regular Expression" option in the replacement dialog box, then you can Completed replacement
[5] Regular expression application - delete blank lines
Start EditPlus and open the text type file to be processed.
① Select the "Replace" command in the "Find" menu to pop up the text replacement dialog box. Select the "Regular Expression" checkbox to indicate that we want to use regular expressions in search and replacement. Then, select "Current File" in "Replacement Range" to indicate the operation on the current file.
②. Click the button on the right side of the "Find content" combo box, and a drop-down menu will appear.
③. The following operation adds a regular expression, which represents the empty line to be found. (Tip: Blank lines only include space characters, tab characters, and carriage return characters, and must start with one of these three symbols as a line and end with a carriage return character. The key to finding blank lines is to construct a representation of a blank line. regular expression).
Directly enter the regular expression "^[ /t]*/n" in "Search". Note that there is a space character before /t.
(1) Select "Match from the beginning of the line", and the character "^" appears in the "Find content" combo box, indicating that the string to be found must appear at the beginning of a line in the text.
(2) Select "Characters in range", then a pair of brackets "[]" will be added after "^", and the current insertion point is in the brackets. Brackets are represented in regular expressions. If the characters in the text match any character in the brackets, they meet the search conditions.
(3) Press the space bar to add a space character. The whitespace character is a component of a blank line.
(4) Select "Tab" and add "/t" representing the tab character.
(5) Move the cursor, move the current insertion point after "]", and then select "Match 0 or more times", this operation will add the asterisk character "*". The asterisk means that there are 0 or more spaces or tabs in the brackets "[]" in front of it in one line.
(6) Select "Line Feed" and insert "/n" to represent the carriage return character.
④. The "Replace with" combo box remains empty, indicating that the found content is deleted. Click the "Replace" button to delete empty lines one by one, or click the "Replace All" button to delete all empty lines (Note: EditPlus sometimes has the problem that "Replace All" cannot completely delete empty lines at one time. It may be a program BUG. You need to Press the button several times).
1. When translating into Chinese, do you often encounter sentences like this that need to be translated:
Code:
“Error adding the post!”;
“Error adding the comment!”;
"Error adding the user!";
If there are many similar files, translating them one by one will obviously be tiring and boring.
In fact, it can be handled like this, use the replacement function in Editplus, and select the "Regular Expression" checkbox in the replacement dialog box:
Find the original file:
Code:
"Error adding ([^ !|"|;]*)
Replaced with:
Code:
"An error occurred while adding /1
What happened after this replacement? The result is:
Code:
"An error occurred while adding the post!";
"An error occurred while adding the comment!";
"An error occurred while adding the user!";
ok, what will you do next? Do? Of course, replace the post, the comment, and the user with the words you want to translate. Get the final result:
Code:
"An error occurred while adding a post!";
" An error occurred while adding a comment!";
"An error occurred while adding a user!";
2. The word to be extracted is in the middle, such as:

Code: 
 can not be deleted because 
 can not be added because 
 can not be updating because

可以用这种方式: 
在Editplus里面用 替换 功能,在替换对话框选中“正则表达式”复选框: 
查找原文件: 
Code: 
can not be ([^ ]*) because 
替换成: 
Code: 
无法被/1因为 
这样替换之后发生了什么?结果是: 
Code: 
无法被deleted因为 
无法被added因为 
无法被updating因为 
其余步骤如上。 
在汉化量很大而且句式比较单调的情况下对效率的提高很明显! 
解释一下:([^!|"|;]*) 的意思是 不等于 ! 和 ” 和 ; 中的任何一个,意思就是这3个字符之外的所有字符将被选中(替换区域); 
/1 即被选中的替换区域所在的新位置(复制到这个新位置)。 
3.经常手工清理一行一行地删除文本文件里面的空白行,其实可以交给Editplus更好的完成,在Editplus里面用替换功能,在替换对话框选中 “正则表达式”复选框: 
查找原文件: 
Code: 
^[ /t]*/n 
替换部分为空就可以删除空白行了,执行一下看看:) 
abandon[2''b9nd2n]v.抛弃,放弃 
abandonment[2''b9nd2nm2nt]n.放弃 
abbreviation[2bri:vi''ei62n]n.缩写 
abeyance[2''bei2ns]n.缓办,中止 
abide[2''baid]v.遵守 
ability[2''biliti]n.能力 
able[''eibl]adj.有能力的,能干的 
abnormal[9b''n0:m2l]adj.反常的,变态的 
aboard[2''b0:d]adv.船(车)上 

1. 
查找: (^[a-zA-Z0-0/-]+)(/[*.*/]+)(.*) 
替换: @@@@@”/1″,”/2″,”/3″, 
效果: 
@@@@@”abandon”,”[2''b9nd2n]“,”v.抛弃,放弃”, 
@@@@@”abandonment”,”[2''b9nd2nm2nt]“,”n.放弃”, 
@@@@@”abbreviation”,”[2bri:vi''ei62n]“,”n.缩写”, 
@@@@@”abeyance”,”[2''bei2ns]“,”n.缓办,中止”, 
@@@@@”abide”,”[2''baid]“,”v.遵守”, 
@@@@@”ability”,”[2''biliti]“,”n.能力”, 
@@@@@”able”,”[''eibl]“,”adj.有能力的,能干的”, 
@@@@@”abnormal”,”[9b''n0:m2l]“,”adj.反常的,变态的”, 
@@@@@”aboard”,”[2''b0:d]“,”adv.船(车)上”, 

2. 
查找: /n 
替换: 
注: 要次替换内容为空 
效果: 
@@@@@”abandon”,”[2''b9nd2n]“,”v.抛弃,放弃 ”,@@@@@”abandonment”,”[2''b9nd2nm2nt]“,”n.放弃 ”,@@@@@”abbreviation”,”[2bri:vi''ei62n]“,”n.缩写 ”,@@@@@”abeyance”,”[2''bei2ns]“,”n.缓办,中止”,@@@@@”abide”,”[2''baid]“,”v.遵守 ”,@@@@@”ability”,”[2''biliti]“,”n.能力”,@@@@@”able”,”[''eibl]“,”adj.有能力的,能 干的 ”,@@@@@”abnormal”,”[9b''n0:m2l]“,”adj.反常的,变态的 ”,@@@@@”aboard”,”[2''b0:d]“,”adv.船(车)上”,@@@@@”abolish”,”[2''b0li6]“,”v.废 除,取消”,@@@@@”abolition”,”[9b2''li62n]“,”n.废除,取消” 

3. 
查找: @@@@@ 
替换: /n 
效果: 
“abandon”,”[2''b9nd2n]“,”v.抛弃,放弃”, 
“abandonment”,”[2''b9nd2nm2nt]“,”n.放弃”, 
“abbreviation”,”[2bri:vi''ei62n]“,”n.缩写”, 
“abeyance”,”[2''bei2ns]“,”n.缓办,中止”, 
“abide”,”[2''baid]“,”v.遵守”, 
“ability”,”[2''biliti]“,”n.能力”, 
“able”,”[''eibl]“,”adj.有能力的,能干的”, 
“abnormal”,”[9b''n0:m2l]“,”adj.反常的,变态的”, 
“aboard”,”[2''b0:d]“,”adv.船(车)上”, 
“abolish”,”[2''b0li6]“,”v.废除,取消”, 

4. 任务完成

一、删除空行(不包括有空格类符号的空行) 
1、\r\n转义符替换 
按ctrl+h,跳出搜索替换框,把查找模式定义为扩展(\n,\r...) 
查找目标:\r\n\r\n 
替换为:\r\n

有编程基础的读者应该知道是什么意思了。

2、Textfx插件 
先选中要删部分文本内容,如果是整个文件那就全选Ctrl+A,然后使用Notepad++自带的Textfx插件,在长长的列表中找到Delete Blank Lines,点击即可。

Note that Notepad's regular expressions are incompatible with escape characters, etc., so they have great limitations and cannot be directly replaced with regular expressions.

2. Delete empty lines with spaces
1. Delete spaces first, then delete empty lines
How to delete lines with only spaces?
Find Blank Operations (line editing) in the menu editing, click to remove the blank at the end of the line, and then use the above method to delete the blank line.

2. Use regular expressions to delete blank lines and spaces
Select the regular expression ^ $ for the search mode in Replacement, replace it with nothing (that is, fill in nothing), and then use the above method to delete the empty lines.

The above is the detailed content of About notepad++ regular expression replacement string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete