Minimal Regexp Matches, How To?

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Martin Fabian

Minimal Regexp Matches, How To?

Post 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?
Andreas

Re: Minimal Regexp Matches, How To?

Post by Andreas »

o[^e]*e

[^e] matches anything but an e
Martin Fabian

Re: Minimal Regexp Matches, How To?

Post 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?)
Jens Hollmann

Re: Minimal Regexp Matches, How To?

Post 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 :-)
Post Reply