Page 1 of 1

Minimal Regexp Matches, How To?

Posted: Fri Nov 30, 2001 11:36 am
by Martin Fabian
How do I perform a minimal character regexp match?

Example:
For the string "one two three" the regexp "o.*e" matches the entire string. However, I want a regular expression that matches the shortest substring beginning with "o" and ending with "e", that is "one" in this case (with the next match being "o thre" and not "o three").

Anyone knows? Is it at all possible?

Re: Minimal Regexp Matches, How To?

Posted: Sat Dec 01, 2001 10:40 am
by Andreas
o[^e]*e

[^e] matches anything but an e

Re: Minimal Regexp Matches, How To?

Posted: Sat Dec 01, 2001 8:54 pm
by Martin Fabian
At 10:40 2001-12-01 GMT, Andreas wrote:
>
>o[^e]*e
>
>[^e] matches anything but an e
>
Sure, this solves it for this specific problem.
I had something in general in mind.
Is there a way to do minimal matching _in_general_?
(Or can it be shown that the problem can always be solved like this?)

Re: Minimal Regexp Matches, How To?

Posted: Mon Dec 03, 2001 8:42 am
by Jens Hollmann
If you have only a character at the end of the expression you search for, it's always possible to do it in the above way. But in

one and one and one

searching for the shortest expression beginning with "o" and ending with "and" is not possible.

This is called "greedy" regular expression and it's the default in TextPad (as in most other editors I know).

Perl knows special regexps that match non-greedy.

So I don't think this is possible in general. So send it as an enhancement request :-)