A re is a string of symbols used to match a particular substring of a string. Whether it’s to validate user input or to replace text in batches, it’s a very efficient tool. Because re has its special syntax, it is difficult to learn, so there is an old saying: when you try to use re to solve a problem, you have two problems.
Let’s look at some of the basic concepts in re.
-
Or | like the inside of the JS | | said or mean, gray | gray or grey grey said
-
Group of the inside of the parentheses () content is expressed as a set, gray | grey = = = gr (a) | e y
-
{n} indicates n times, {n} curly braces indicate n times, {min,} indicates min times or more, {, Max} indicates Max times or once, {min, Max} indicates the closed interval between min and Max
-
? According to zero or a = = = {0, 1}, * said any, zero or more = = = {0}, + said at least one = = = {1},
-
[] indicates a single character, [ABC] [a-b] indicates a, B, or C, and [^ ABC] indicates any character other than a, B, or C
Greed mode
, +,? The default is greedy mode. For example, “test”, “test1234 “, “.+”, or “. * “will match the entire line “test”, “test1234 “, skipping the close quotation mark, only the last quotation mark, and adding? It then goes into non-greedy mode, ending when the first closing quote is matched, and “[^”]*” achieves the same greedy effect.
Exclusive mode
“[^”]*+” with a plus sign will only match “test”, because the plus sign will match as many rules as possible until failure ends, preventing backtracking. Unlike non-greedy mode, use? The non-greedy mode will match the current rule as little as possible, and only backtrack when there is no match. Non-greedy mode is similar to exclusive mode, which saves time and performance, but may not meet the requirements. It is worth noting that the Python and Go standard libraries do not support exclusive mode, requiring module installation.
Backtracking: for example ab{1,3}? C matches abbc, matches ab because? Use b to match c as little as possible and go back to abb to match ab{1,3} if you find a mismatch. Ab {1,3}}+c matches abb as many times as possible, and then c matches abb, and then c matches b, and then c matches c.