When we use a regular expression to match a string, the regex greedily tries to look for the longest pattern possible in the string.
Examaple:
when you specify the pattern 'ab{2,7}' to match the string 'abbbbbbb', it will look for the maximum number of occurrences of 'b' (in this case 7).This is called a 'greedy approach'. By default, a regular expression is greedy in nature.
There is another approach called the non-greedy approach, also called the lazy approach, where the regex stops looking for the pattern once a particular condition is satisfied.
Examaple:
when you specify the pattern 'ab{2,7}' to match the string 'abbbbbbb', it will look for the maximum number of occurrences of 'b' (in this case 7).This is called a 'greedy approach'. By default, a regular expression is greedy in nature.
There is another approach called the non-greedy approach, also called the lazy approach, where the regex stops looking for the pattern once a particular condition is satisfied.
Code Example:
No comments:
Post a Comment