This example is from mastering regular expressions. Take notes to help you understand and remember.
First version
The simplest case is to consider including a pair of quotation marks, so the written expression should be like this:
".*"
But it's too easy. What's the problem? If the input string is long, the result will be pulled out. See...
Input String: "Hello" and "World" Regex: ".*" Match: "Hello" and "World"
Why are all matched? This is because * is a greedy (matching priority) quantizer. I think the meaning of English is easier for us to understand. This means that it will first 'gree' all the characters to be matched, matching the last character to find that no character can be matched, so it starts to match the next quotation mark, it first goes back to the last quotation mark, then starts matching the quotation mark to find that it can be matched, and then completes. This is why the entire string is matched.
Version 2
Since this problem is caused by greedy quantifiers, we will give it to lazy (ignore the priority) quantifiers -----*? . When a quantizer ignores precedence, the engine will ignore the character that ignores the precedence modifier to match the next character, if not, the system returns a match for the character that ignores the preference modifier.
Let's take a look at this example. If the expression is slightly modified, the matching result becomes a bird.
Input String: "Hello" and "World" Regex: ".*?" Match-1: "Hello" Match-2: "World"
Now, it seems that this version has successfully completed our task.
Third Edition
Then we will continue to change our requirements. In the world of programs, there is something called transfer character, for example, there is a string long.\"Hello, World!\"+\". Directly use the final regular expression.
Input String: \"Hello, World!\"+\" Regex: "(\\.|[^\\"])*" Match-1: No Match
The key in a regular expression is this(\\.|[^\\"])In the regular expression, this bracket acts as a multi-choice structure, indicating matching any sub-expression in the brackets. There are two parts in the brackets. The first part is\\., The second part is[^\\"].
\\.
\\.It can match any escape character. Strictly speaking, it can match any \ and the characters following it, even if they are not real escape characters.
[^ \ "]
The last quotation mark of the input string is the transferred quotation mark, so this regular expression does not match any result. This result is correct, and the credit is due to this[^\\"]It means matching any character of non. When the engine matches the string"If no match exists, the system traces back to it? That location\"Hello, World!\"+ ? \". The engine will try to match[^\\"]And the matching fails!
Assume that[^\\"]Change[^"]In that position,[^"]Will match\Character, and then\After"Will be matched by the outer quotation marks, so the result is matched. This is obviously quite strange. Of course, if you want this match, you can.
Other Versions
Another way is to use the atomic grouping in the regular expression or possessive ). Curing group:"(?>(\\.|[^"])*), Takes precedence:"(\\.|[^"])*+". If a character is already matched in a fixed group or in a group with a given priority modifier, the previous state will be discarded, to prevent backtracking.
In this example,\"Hello, World!\"+\"When this position is matched (\"Hello, World!\"+\" ?) If no matching method can be found, the engine will choose to directly fail the matching instead of going back to the previous standby status for new matching.
Key concepts
Although the example is simple, there are many key concepts in regular expressions. For more information, see mastering regular expressions.
- Greedy quantifier)
- Lazy quantifier)
- Possessive quantifier)
- Atomic grouping)
- Character group (character classes)
- Alternation)
This document is purely personal reading notes. You are not responsible for any errors ~~~