Notepad ++ regular expression)

Source: Internet
Author: User
Using Regular Expressions can well complete a lot of tedious and time-consuming work. The following transcription editplus regular expressions are also applicable to notepad ++:
Expression description
\ T tab.
\ N new line.
. Match any character.
| Match the characters on the left and right of the expression. For example, "AB | BC" matches "AB" or "BC ".
[] Match any single character in the list. For example, "[AB]" matches "A" or "B". "[0-9]" matches any number.
[^] Matches any single character out of the list. for example, "[^ AB]" matches characters other than "A" and "B. "[^ 0-9]" matches any non-numeric characters.
* The character on the left is matched any time (0 or multiple times). For example, "Be *" matches "B", "be", or "Bee ".
+ The character on the left is matched at least once (once or multiple times). For example, "Be +" matches "be" or "Bee" but does not match "B ".
? The character on the left is matched 0 times or 1 time. For example, "be? "Match" B "or" be "but not" Bee ".
^ The expression on the right is matched at the beginning of a row. For example, "^ A" only matches rows starting with ".
$ The expression on the left is matched at the end of a row. For example, "e $" only matches rows ending with "E.
() Affects the order of expression matching and is used as the grouping tag of the expression.
\ Escape character. If you want to use "\" itself, you should use "\\".

Example:

Original string
STR [1] ABC [991];
STR [2] ABC [992];
STR [11] ABC [993];
STR [22] ABC [2, 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] apply a regular expression -- replace the specified content with the end of the row
The original text is shown in the following two lines
ABC aaaaa
123 ABC 444

If you want to encounter "ABC" each time, replace "ABC" and the content from the end to the end of the line with "abc efg"
That is, the above text is replaced:
ABC EFG
123 ABC EFG

Solution:
① In the replace dialog box, enter "ABC. *" in the search content .*"
② Select the "Regular Expression" check box and click "replace all ".
The meaning of the symbol is as follows:
"." = Match any character
"*" = Matches 0 times or more

Note: It is actually a replacement of regular expressions. Here we just sort out some previously raised questions. simply from the regular expression itself, we can extend thousands of special cases.

[2] Regular Expression application-digit replacement
We hope
Asdadas123asdas456asdasd789asdasd
Replace:
Asdadas [1, 123] asdasdas [2, 456] asdasdasd [2, 789] asdasd

In the replace dialog box, select the regular expression check box;
Enter "[0-9] [0-9] [0-9] [0-9]" in the search content, without quotation marks
"Replace with:" And enter "[\ 0 \ 1 \ 2]" without quotation marks.
The range is the range for your operation, and then you can choose to replace it.

In fact, this is a special case for regular expressions. "[0-9]" indicates matching 0 ~ For any special case between 9, "[A-Z]" indicates matching ~ Any special case between Z
The preceding statement uses "[0-9]" to indicate three consecutive numbers.
"\ 0" indicates the prototype corresponding to the first "[0-9]", and "\ 1" indicates the prototype corresponding to the second "[0-9 ]".
"[", "]" Is a simple character, indicating to add "[" or "]". If you enter "Other \ 0 \ 1 \ 2", the replacement result is:

Asdadas other 123 other asdasdas other 456 other asdasdasd other 789 other asdasd

Function enhancement (by jiuk2k ):
If you change "[0-9] [0-9] [0-9]" to "[0-9] * [0-9]", corresponding to 1, 123, 12345, or...
Customized as needed

There are a lot of related content. You can refer to the regular expression syntax for a closer look.

[3] Regular Expression Application -- delete the specified character at the end of each row
These characters also appear in the row, so they cannot be simply replaced.
For example
12345 1265345
2345
Delete "345" at the end of each row"
This is also the usage of the regular expression. In fact, it should be relatively simple to look at the regular expression carefully. However, since this problem is raised, it indicates that there is still a process of understanding the regular expression. The solution is as follows:
Solution:
In the replace dialog box, enable the regular expression check box.
Enter "345 $" in the search content"
"$" Indicates matching from the end of the line

If match from the beginning of the line, you can use "^" to implement it. However, editplus has another function that can easily Delete the string at the beginning of the line.
A. Select the row to be operated.
B. Edit-format-Delete row comment
C. In the pop-up dialog box, enter the first character of the row to be cleared. OK.

[4] Regular Expression Application -- replace multiple rows with parentheses
The following section is available in hundreds of webpages:Code:
\ N
In the replace dialog box, enable the regular expression option.

[5] Regular Expression Application -- delete empty rows
Start editplus to open a text file to be processed.
① Select the "replace" command in the "Search" menu to bring up the text replacement dialog box. Select the "Regular Expression" check box, indicating that we want to use a regular expression in search and replacement. Then, select "current file" in "replacement range" to operate the current file.
2. click the button on the right of the "search content" combo box. A drop-down menu is displayed.
③ The following Operation adds a regular expression, which represents the blank row to be searched. (Tip: A blank line only contains space characters, tabs, and carriage returns. It must start with one of the three symbols and end with a carriage return, the key to searching for empty rows is to construct a regular expression that represents empty rows ).
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 "search content" combo box, indicating that the string to be searched must appear at the beginning of the line in the text.
(2) If you select "character in the range", a pair of brackets "[]" will be added after "^". The current insertion point is in the brackets. In regular expressions, brackets indicate that any character in the text matches the matching conditions.
(3) press the Space key 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 to "]", and select "Match 0 times or more". This operation adds the asterisk (*). Asterisk indicates the space character or Tab character in the brackets "[]" in front of it. There are 0 or more characters in a row.
(6) Select "linefeed" and insert "\ n" to indicate a carriage return.
④ The "Replace with" combo box is left empty, indicating that the searched content is deleted. Click "replace" to delete empty rows one by one, or click "replace all" to delete all empty rows (Note: editplus sometimes has the problem that "replace all" cannot completely delete empty rows at a time, which may beProgramBug, you need to press the button several times ).

1. Do you often encounter such statements in Chinese:

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

If there are many similar files, one translation is obviously very tired and boring.

In fact, you can do this by using the replace function in editplus. In the replace dialog box, select the "Regular Expression" check box:
Find the original file:

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

Replace:

Code:
"An error occurred when adding \ 1.

What happens after this replacement? The result is:

Code:
"An error occurred when adding the post !";
"An error occurred when adding the comment !";
"An error occurred when adding the user !";

OK. What will you do next? 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 comments !";
"An error occurred when adding users !";

2. The words to be extracted are in the middle, for example:

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

You can use this method:
Use the replace function in editplus. In the replace dialog box, select the "Regular Expression" check box:
Find the original file:

Code:
Can not be ([^] *) because

Replace:

Code:
Cannot be \ 1 because

What happens after this replacement? The result is:

Code:
Cannot be deleted because
Cannot be added because
Cannot be updating because

The remaining steps are as follows.

The improvement in efficiency is obvious when the number of Chinese characters is large and the sentence structure is monotonous!

Explain: ([^! | "|;] *) Means not equal! And "and;" indicates that all characters other than the three characters will be selected (replacing the region );
\ 1 is the new location of the selected replacement area (copied to this new location ).

3. you can delete the blank lines in a text file by hand. You can use the replace function in editplus. In the replace dialog box, select the "Regular Expression" check box:
Find the original file:

Code:
^ [\ T] * \ n

The blank line can be deleted after the replacement is null. Execute the following command to see if :)

Abandon [2 ''b9nd2n] v. Abandon and give up
Abandonment [2 ''b9nd2nm2nt] n. Give up
Abbreviation [2bri: VI ''ei62n] n. Abbreviations
Abeyance [2' bei2ns] n. Stop
Abide [2 ''baid] v. Follow
Ability [2 ''' biliti] n. Capability
Able [''eibl] adj. Competent and competent
Abnormal [9b' N0: m2l] adj. Abnormal, abnormal
Aboard [2 ''B0: d] adv. Boat (CAR) on

1.
Search: (^ [a-zA-Z0-0 \-] + )(\[*. * \] + )(. *)
Replace: @ "\ 1", "\ 2", "\ 3",
effect:
@ "abandon", "[2' b9nd2n]", "V. abandon, discard ",
@" abandonment "," [2 ''b9nd2nm2nt]", "n. discard ",
@" abbreviation "," [2bri: VI ''ei62n]", "n. abbreviation ",
@" abeyance "," [2' bei2ns] "," n. stop ",
@" abide "," [2 ''baid]", "V. follow ",
@" ability "," [2' biliti] "," n. capability ",
@" able "," [''eibl] "," adj. competent and competent ",
@" abnormal "," [9b'' N0: m2l] "," adj. abnormal, abnormal ",
@" aboard "," [2 ''B0: d]", "adv. ship (vehicle) ",

2.
Search: \ n
Replace:
note: the content to be replaced is blank.
effect:
@ "abandon ", "[2' b9nd2n]", "V. abandon, give up ", @" abandonment "," [2' b9nd2nm2nt] "," n. discard ", @" abbreviation "," [2bri: VI ''ei62n]", "n. abbreviation "," abeyance "," [2' bei2ns] "," n. stop ", @" abide "," [2 ''baid]", "V. follow ", @" ability "," [2' biliti] "," n. capability ", @" able "," [''eibl] "," adj. ", @" abnormal "," [9b'' N0: m2l] "," adj. abnormal, abnormal ", @" aboard "," [2 ''B0: d]", "adv. ship (vehicle) to ", @" Abolish "," [2' b0li6] "," V. cancel ", @" Abolition "," [9b2 ''li62n]", "n. abolish, cancel "

3.
Search:
Replace: \ n
effect:
"abandon", "[2' b9nd2n]", "v. abandon, give up ",
" abandonment "," [2' b9nd2nm2nt] "," N. abandon ",
" abbreviation "," [2bri: VI ''ei62n]", "N. abbreviation ",
" abeyance "," [2' bei2ns] "," n. ",
" abide "," [2' baid] "," V. comply with ",
" ability "," [2' biliti] "," n. capability ",
" able "," [''eibl]", "adj. competent and competent ",
" abnormal "," [9b''n0: m2l] "," adj. unusual, abnormal ",
" aboard "," [2' B0: d] "," adv. ship (vehicle) ",
" Abolish "," [2' b0li6] "," V. abolished, canceled ",
original address: http://www.cnblogs.com/answer/archive/2010/03/29/1699751.html

now I have a bunch of Chinese characters in my hand, which are 34 provinces, cities, and autonomous regions in China (I have heard that there are 36 Chinese characters, and 34 Chinese characters are queried), as shown below:
Beijing
Tianjin
Shanghai
...
change it to the following:
insert into province_info (province_name) values ('beijing');
insert into province_info (province_name) values ('tianjin ');
insert into province_info (province_name) values ('shanghai');
...
place "Beijing" in the single quotes of the following statement.
insert into province_info (province_name) values ('');
Replace the statement, select a regular expression to search for the target
(. *)
replace
insert into province_info (province_name) values ('\ 1');
find the target. * You can find the target, and add () to use \ 1 for replacement.
This allows you to quickly replace it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.