Notepad++ Regular Expression Replacement String Details

Go to http://blog.csdn.net/yf210yf/article/details/38345561The


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

EditPlus' Find, Replace, and Find in Files supports the following regular expressions:

expression specification
/t tab.
/n new line.
. matches any character.
| matches the characters to the left and right of the expression. For example, "ab| bc" matches "ab" or "bc".
[] matches any single character in the list. For example, "[ab]" matches "a" or "b". "[0-9]" matches any number.
[ ^] matches any single character outside the list. For example, "[^ab]" matches characters other than "a" and "b". "[^0-9]" matches any non-numeric character.
* The character to the left of it Matched any number of times (0, or more). For example "be*" matches "b", "be" or "bee".
The character to the left of + is matched at least once (1, or more). For example "be+" matches "be" or "bee" but not "b".



() affects the order in which expressions are matched and is used as a grouping marker for expressions.
/ is the escape character. If you want to use "/" by itself, you should use "//".

Example:

raw string
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];

target string:
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] Regular Expression application - replace the specified content to the end of the line The
original text is the following two lines
abc aaaaa
123 abc 444 If

you want to encounter "abc" every time, replace "abc" and the content after it to the end of the line with "abc efg"
That is, the above text is finally replaced with:
abc efg
123 abc efg

Solution:
① In the Replace dialog box, enter "abc.*" in the Find content
② Check the "Regular Expression" checkbox at the same time, and then click the "Replace All" button
Among them, the meanings of the symbols are as follows:
"." = matches any character
"*" = matches 0 or more times

Note : In fact, it is a regular expression replacement, here is just to sort out some questions that have been raised, simply from the regular expression itself For example, thousands of special cases can be derived.

[2] Regular expression application - number replacement
I want to replace
asdadas123asdasdas456asdasdasd789asdasd
with:
asdadas[123]asdasdas[456]asdasdasd[789]asdasd In the replacement dialog box, check

the "regular expression" checkbox;
Enter "[0-9][0-9][0-9]" in it, without quotation marks
"replace with:" and enter "[/0/1/2]" in it, without quotation marks, the
range is what you operate range, 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 "[az]" means to match any special case between a and z. Repeated use
above "[0-9]", indicating three consecutive numbers
"/0" represents the prototype corresponding to the first "[0-9]", "/1" represents the second "[0-9]" corresponding prototype, and so on
"[", "]" are simple characters, which means adding "[" or "]", if you enter "other/0/1/2 other", the replacement result will be:

asdadas other 123 other asdasdas other 456 other asdasdasd other 789 Other asdasd

function enhancements (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 still many

related contents. You can study the grammar of regular expressions carefully

[3] Regular expression application - delete the specified characters at the end of each line
because these characters are in the line It also appears in , so it must not be implemented by simple replacement. For
example
12345 1265345
2345
needs to delete the "345" at the end of each line.
This is also the usage of regular expressions. In fact, it should be relatively simple to look at regular expressions carefully, but since there is this problem The
solution :
In the Replace dialog box, enable the "Regular Expression" check box Enter "345$"
in the Find Content,
where "$" means from the line End matching

If you from the beginning of the line, you can use "^" to achieve, but EditPlus has another function that can easily delete the string at the beginning of the line
a. Select the line to operate
b. Edit - format - delete line comment
c. In the pop-up dialog box, enter the first character of the line to be cleared, and click OK

[4] Regular expression application - replace multiple lines with half-width brackets
Hundreds of web pages have the following code:
/nEnable
the "regular expression" option in the replacement dialog box, and then the replacement can be completed

[ 5) Regular expression application - delete blank lines
Start EditPlus and open the text type file to be processed.
①. Select the "Replace" command of the "Find" menu, and a text replacement dialog box will pop up. Checking the "Regular Expression" checkbox indicates that we want to use a regular expression in find and replace. Then, select "Current File" in "Replace Range" to indicate the operation on the current file.
②. Click the button on the right side of the "Find what" combo box, and a drop-down menu will appear.
3. The following operations add a regular expression, which represents the blank line to be searched. (Tips: Empty lines only include spaces, tabs, and carriage returns, and must start a line with one of these three symbols and end with a carriage return. The key to finding empty lines is to construct representations of empty lines regular expression).
Directly enter the regular expression "^[ /t]*/n" in "Find", pay attention to the space character before /t.
(1) Select "match from the beginning of the line", the character "^" appears in the "Find what" combo box, indicating that the string to be searched must appear at the beginning of a line in the text.
(2) Select "Character in range", then a pair of brackets "[]" will be added after "^", and the current insertion point is in the brackets. Parentheses are expressed in regular expressions, and the characters in the text match any character in the parentheses to meet the search conditions.
(3) Press the space bar to add a space character. A space character is a component of a blank line.
(4) Select "Tab" and add "/t" representing the tab.
(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 indicates that there are zero or more spaces or tabs within the brackets "[]" in front of it on a line.
(6) Select "Line Feed" and insert "/n" to indicate carriage return.
④. The "Replace with" combo box remains empty, which means to delete the found content. 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 localizing, do you often encounter such sentences that need to be translated:

Code:
"Error adding the post!";
"Error adding the comment!";
"Error adding the user!";

If there are many similar files, one A translator was clearly tired and bored.

In fact, it can be handled like this, use the replace function in Editplus, select the "regular expression" check box in the replacement dialog box:
find the original file:

Code:
"Error adding ([^!|"|;]*)

Replace with:

Code :
"Error while adding /1 What happened after

this substitution? The result is:

Code:
"Error adding the post!";
"Error adding the comment!";
"Error adding the user!";

ok, what do you do next? 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 when adding a post!";
"An error occurred when adding a comment!";
"An error occurred when adding a user!";

2. The word to be extracted is in the middle, for example:

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

you can use this method:
use the replace function in Editplus, select the "regular expression" check box in the replacement dialog box:
find the original file:

Code:
can not be ([^ ]*) because is

replaced by:

Code:
cannot be replaced by /1 because what happens after

this replacement? The result is:

Code:
cannot be deleted because
cannot be added because
cannot be updated because

the rest of the steps are as above.

In the case of a large amount of sinicization and monotonous sentence patterns, the improvement of efficiency is obvious!

Explain: ([^!|"|;]*) means not equal to any of ! and " and ;, meaning that all characters except these 3 characters will be selected (replacement area);
/1 That is, the new location where the selected replacement area is located (copied to this new location).

3. Often manually clean up line by line to delete the blank lines in the text file. In fact, it can be done better by Editplus. Use the replace function in Editplus, and select the "regular expression" checkbox in the replace dialog box:
find the original file :

Code:
^[ /t]*/n

If the replacement part is empty, you can delete blank lines, execute it and see:)

abandon[2''b9nd2n]v. abandon, abandon
abandonment[2''b9nd2nm2nt]n. abandon
abbreviation[2bri:vi''ei62n]
n.abbreviation abeyance[2''bei2ns]n.delay, stop
abide[2''baid]v.obey
ability[2''biliti]n.ability
[''eibl ]adj. Capable, competent
abnormal[9b''n0:m2l]adj. Abnormal, abnormal
aboard[2''b0:d]adv. On the boat (car)

1.
Find: (^[a- zA-Z0-0/-]+)(/[*.*/]+)(.*)
replace: @@@@@”/1″,”/2″,”/3″,

@@@@@"abandon","[2''b9nd2n]","v.abandon, give up",
@@@@@"abandonment","[2''b9nd2nm2nt]","n.abandon",
@@@@@"abbreviation","[2bri:vi''ei62n]",
"n.abbreviation",@@@@@"abeyance","[2''bei2ns]","n.delay, abort",
@@@@@"abide","[2''baid]","v.compliance", @@@@@
"ability","[2''biliti]","n.ability" ,
@@@@@"able","[''eibl]","adj. capable, competent",
@@@@@"abnormal","[9b''n0:m2l]"," adj. Abnormal, perverted",
@@@@@"aboard","[2''b0:d]","adv. On the boat (car)",

2.
Find: / nReplace
:
Note: To The replacement content is empty
Effect :
@@@@@"abandon","[2''b9nd2n]","v. abandon, abandon",@@@@@"abandonment","[2''b9nd2nm2nt]","n. abandon", @@@@@"abbreviation","[2bri:vi''ei62n]","n.abbreviation",@@@@@"abeyance","[2''bei2ns]","n.delay, abort",@@@@@"abide","[2''baid]","v.compliance",@@@@@"ability","[2''biliti]","n.ability" ,@@@@@"able","[''eibl]","adj. capable, capable",@@@@@"abnormal","[9b''n0:m2l]"," adj. Abnormal, perverted",@@@@@"aboard","[2''b0:d]","adv.on the boat (car)",@@@@@"abolish","[ 2''b0li6]","v. repeal, cancel",@@@@@"abolition","[9b2''li62n]","n. repeal, cancel" 3.Find

:
@@@@@
Replace : /n
Effects:
"abandon","[2''b9nd2n]","v.abandon,abandon",
"abandonment","[2''b9nd2nm2nt]","n.abandon",
"abbreviation"," [2bri:vi''ei62n]",
"n.abbreviation","abeyance","[2''bei2ns]","n.delay, abort",
“Abide”, ”[2''baid]“, ”v. Compliance”,
“ability”, ”[2''biliti]“, ”n. Ability”,
"able","[''eibl]","adj.competent, competent",
"abnormal","[9b''n0:m2l]","adj.abnormal, perverted",
"aboard ","[2''b0:d]","adv.Boat (vehicle)",
"abolish","[2''b0li6]","v.Abolished, canceled",

4. Task completed

1 . Delete empty lines (excluding empty lines with space symbols)
1. Replace \r\n with escape characters
Press ctrl+h to jump out of the search and replace box, and define the search mode as expansion (\n, \r...)
Search target: \r\n\r\n
Replace with: \r\nReaders
with programming foundation should know what it means.
2. Textfx plug-in
First select the part of the text to be deleted, if it is the entire file, select Ctrl+A, then use the Textfx plug-in that comes with Notepad++, find Delete Blank Lines in the long list, and click.
Note that Notepad++'s regular expressions are not compatible with escape characters, etc., so they are limited and cannot be replaced directly with regular expressions.
2. Delete blank lines with spaces
1. Delete spaces first, then delete blank lines
How to delete blank lines with only spaces?
Find Blank Operations (line editing) in the menu editor, 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 ^ +$ in the search mode in the replacement, replace it with empty (that is, fill in nothing), and then use the above method to delete the blank lines.
The above methods have been introduced, and the columns are more detailed, so that you can deduce other special uses when searching and replacing. You are welcome to leave a message to add.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326273145&siteId=291194637