notepad++ regular expression to find specific string

How to process batch text

There are many indicators and values ​​in the message that have fixed formats, such as a="1". If we only take the previous indicator a, it will be more complicated, but using regular expressions will be much happier!

Use the second method below

Search target = (.+?)\" means to search for a string starting with an equal sign and ending with a quotation mark and a space. This can avoid finding the first quotation mark, and then replace it with \n in batches, that is, each indicator is individually wrapped and listed.

1. Regular expression matches this line of data starting with a certain character

Expression:
(?:^|\n)Character position.*     

Example:
(?:^|\n)prompt.* represents the line of string starting with prompt.

2. Regular expression matches characters starting with string a and ending with string b, regardless of the middle

Expression:
a string position (.+?) b string position. Note that many characters have special meanings and must be escaped with \.
Example:
Match strings starting with to_date and ending with), string
to_date(. +?)\), that's it, be sure to escape ).
Note:
The above mentioned ends with (bracket comma).

3. Only match pure numeric strings

Expression:
^[0-9]+$
Explanation:
^: Matches the beginning of the line
[0-9]+: Matches 1 or more numbers
$: Matches the end of the line, generally speaking, it matches a line of numbers
. Example:
Matches only A line in the document that is entirely filled with numbers, without any symbols.

4. Only match pure letter strings

Expression:
^[A-Za-z]+$
Explanation:
^: Matches the beginning of the line
[A-Za-z]+: Matches 1 or more letters
$: Matches the end of the line, generally speaking, it matches a line of letters
Example:
Only match a line in the document that is full of letters, without any symbols.

5. Find multiple strings at the same time

Expression:
a|b
Example:
Zhang San|Li Si|Wang Wu
Explanation:
Also search for the lines containing the strings of Zhang San, Li Si and Wang Wu in the text.

The difference between regular expressions (.+) and (.+?)

1. Symbol interpretation

① () grouping character, treating the characters in brackets as a whole.

② . Matches all characters except line breaks, for single characters.

③ + The previous character must exist and can be repeated 1 or more times

④? Following the substring means matching the previous string 1 or 0 times, that is, the previous character can exist or not, but can only exist once;

Follow .,+,? After that, it means entering non-greedy mode, also called lazy mode.

Regular default greedy mode

    greedy mode

Match the longest string possible. Greedy matching first checks whether the entire string matches. If it does not match, it removes the last character in the string and tries to match again. This cycle continues until the match is successful.

    non-greedy mode

Match the shortest string possible. Lazy matching is to match from the first character on the left to the right, first check whether it matches, if not, add the next character on the right and try again. This cycle continues until the match is successful.
3. Examples

The string "<1><123>" exists

①Regular expression <(.+)> means to match the longest content that conforms to the rule <string> as much as possible, and finally returns "<1><123>"

②Regular expression <(.+?)> means to match the shortest content that conforms to the rule <string> as much as possible, and finally returns "<1>"
——————————————
Copyright Statement: This article is an original article by CSDN blogger "Bitup_bitwin" and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/weixin_44259499/article/details/129342053

Guess you like

Origin blog.csdn.net/qq_41638825/article/details/131891476