Re: Regexp matching: bug or operator error?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kenneth Tanzer <ktanzer(at)desc(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Regexp matching: bug or operator error?
Date: 2004-11-26 23:39:16
Message-ID: 20226.1101512356@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-docs pgsql-general

Kenneth Tanzer <ktanzer(at)desc(dot)org> writes:
> But what about these two queries:
> SELECT substring('a' FROM 'a?|a?');
> This returns a greedy 'a', similar to the example above. But then why does
> SELECT substring('ba' FROM 'a?|a?');
> return a non-greedy empty string?

You're ignoring the first rule of matching: when there is more than one
possible match, the match starting earliest in the string is chosen.
The longer-or-shorter business only applies when there are multiple
legal ways to form a match starting at the same place. In this case
'a?' can form a legal match at the beginning of the string (ie, match
to no characters) and so the fact that a longer match is available later
in the string doesn't enter into it.

> With regard to the documentation, after re-reading it many times I'd
> have to say the information is all there, but it's hard to absorb.

I'd agree. This section was taken nearly verbatim from Henry Spencer's
man page for the regexp package, and with all due respect to Henry,
it's definitely written in geek reference-page-speak. Maybe a few
examples would help.

On the other hand, I don't want to try to turn the section into a regexp
tutorial --- there are entire books written about regexps (I quite like
the O'Reilly one, btw). So there's a bulk-vs-friendliness tradeoff to
be made.

> I think the main problem is that the term "preference" is used to
> discuss greedy/non-greediness, as well as the words greedy &
> non-greedy.

Good point. It would help to use only one term.

> As an example, here's a couple of different possibilities for the second
> sentence of the section:

I like this one:

> b) If the RE could match more than one substring starting at that point,
> the match can be either greedy (matching the longest substring) or
> non-greedy (matching the shortest substring). Whether an RE is greedy
> or not is determined by the following rules...

Given that intro, there's no need to use the word "preference" at all.
Or almost --- what term will you use for "RE with no preference"?
Perhaps you can avoid the question by pointing out that greediness
only matters for quantifiers, since unquantified REs can only match
fixed-length strings.

The point you make here:

> c) Like individual components of an RE, the entire RE can be either
> greedy (matching the longest substring) or non-greedy (matching the
> shortest substring).

is also important, but probably needs to be a completely separate
paragraph containing its own example.

> Do you think an edit along these lines would be helpful? If so, I'd be
> willing to take a shot at re-writing that section. Let me know. Thanks.

Fire away. Please send whatever you come up with to the pgsql-docs
list.

regards, tom lane

In response to

Responses

Browse pgsql-docs by date

  From Date Subject
Next Message Bruce Momjian 2004-11-27 05:19:46 Re: FAQ and Windows
Previous Message Peter Eisentraut 2004-11-26 22:34:16 Re: [PATCHES] SQL conformance related patch

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-11-26 23:46:54 Re: Bulk data insertion
Previous Message Tom Lane 2004-11-26 23:11:10 Re: pg_dump and languages