DerMajo wrote:Ähm, I'm knowing the usage of google to get the meaning of "non greedy regular expression". But my question was not "Can you explain me the meaning of..." but also "why does my replacement succeed, while you said it wont, because of non-greedy operations".
Its not important to know because my issue is solved. It was just nice to know why it succeed if non-greedy operators not supported.
So far thank you for your help and hints
Bye Majo
The difference between 'greedy" and "non-greedy" is this:
A greedy match will match the LONGEST group of characters possible before continuing to complete matching of the remainder of your regex.
A non-greedy match will match the SHORTEST group of characters possible before continuing to complete matching of the remainder of your regex.
Depending on your regex expression and the text you are searching within, a greedy match and a non greedy match could easily return the same matching text.
So, for example, if you were searching:
(greedy)
Regex=^.*at
Text=The cat found a hat on the boat.
Match="The cat found a hat on the boat"
(non-greedy)
Regex=^.*?at
Text=The cat found a hat on the boat.
Match="The cat"
TextPad doesn't understand a "non-greedy" search, so TextPad will match "The cat found a hat on the boat" in either case.
If your regex expression and the text you are searching within is composed in some particular way, then the "greedy" and "non-greedy" search will return the same matching text, for example:
(greedy)
Regex=^.*on
Text=The cat found a hat on the boat.
Match="The cat found a hat on"
(non-greedy)
Regex=^.*?on
Text=The cat found a hat on the boat.
Match="The cat found a hat on"
So, in your case, if you are certain that the result of TextPad's search/match was correct for ".*?" then it was correct only by a coincidence that TextPad using a greedy-search, matched the same text as your assumption of what a non-greedy-search would have matched.
Basically, it means that if there is only one possible match, then greedy and non-greedy will match the same text. But, if there are two or more possible ways for the search to match, then the non-greedy search will match the SHORTEST of the possible matches, and the greedy search will match the LONGEST of the possible matches.
Kevin