To find the shortest match in a using using regular expressions you can use a “Reluctant” qualifier like “.+?” instead of simply “.+” (greedy qualifier) which will find the longest match.
Wow – did that make my day!! 🙂
To find the shortest match in a using using regular expressions you can use a “Reluctant” qualifier like “.+?” instead of simply “.+” (greedy qualifier) which will find the longest match.
Wow – did that make my day!! 🙂
Comments are closed.
Hi Mikkel,
you possibly mean .* instead of .+
.* : 0, 1 or many (as much as possible)
.+ : 1 or many
.+? : 0, 1 or many (see it as (.+)? : (1 or many) or 0)
Ciao
Thomas
LikeLike
No I actually mean it as a write it – consider some input such as:
If I wanted to use regexp to split the input into two parts with a section in each I would need to use “.+?” and not “.+” to indicate the actual contents between the “Section” and “End Section” parts. If I do the result will be what you expect. I a don’t the result will only be a single match with the entire input.
Try running the below code:
LikeLike
Thanks! I had problems with greedy regex for quite some time. Never imagined that it would be so easy. 🙂
LikeLike
I ran into problems with reluctant qualifiers recently – it seems as if older versions of Python did not implement regex properly. It nearly drove me nuts trying to figure out what was going on. That being said, once I found out about them I went about happily revising old code…
LikeLike