Using Regular Expressions Metacharacters with .net – Greedy and Lazy quantifier
When a regular expression has a quantifier that can accept a range of repetitions (like ".*"), the normal behavior is to match as many characters as possible. Consider the following regular expression:
a.*b The longest string starting with a and ending with b
If this is used to search the string "aabab", it will match the entire string "aabab". This is called "greedy" matching. Sometimes, we prefer "lazy" matching in which a match using the minimum number of repetitions is found. All the quantifiers in Table 2 can be turned into "lazy" quantifiers by adding a question mark "?". Thus "*?" means "match any number of repetitions, but use the smallest number of repetitions that still leads to a successful match". Now let’s try the lazy version of example:
a.*?b The shortest string starting with a and ending with b
If we apply this to the same string "aabab" it will first match "aab" and then "ab".
| Table 1: Regular Expressions Metacharacters Lazy quantifiers | ||
| Regex | RegexDefinition ( or Use) | |
| *? | Repeat any number of times, but as few as possible | |
| +? | Repeat one or more times, but as few as possible | |
| ?? | Repeat zero or one time, but as few as possible | |
| {n,m}? | Repeat at least n, but no more than m times, but as few as possible | |
| {n,}? | Repeat at least n times, but as few as possible | |





Comments
No comments yet.
Leave a comment