Friday, January 22, 2021

Greedy vs Non Greedy search

 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.

Code Example: 




No comments:

Post a Comment

Element wise operation on LIST vs ARRAY

The use of arrays over lists: You can write  vectorised  code on numpy arrays, not on lists, which is  convenient to read and write, and con...