greedy or not? regexps...

Started by Anton Melserabout 19 years ago3 messagesgeneral
Jump to latest
#1Anton Melser
melser.anton@gmail.com

Hi,
I am trying to understand the function substring.

Here:
select SUBSTRING(cit1.summary, '=([0-9]*)') from cms_items cit1
where cit1.summary ~* '.*linkadministration.*[0-9]*';

gives me two empty strings and

select SUBSTRING(cit1.summary, '=([0-9]{1,10})') from cms_items cit1
where cit1.summary ~* '.*linkadministration.*[0-9]*';

gives me the right values.

However, bizarrely,

select SUBSTRING(ban1.url_id, '=([0-9]*)')
from banner ban1
where ban1.url_id ~* '.*linkadministration.*[0-9]*';

select SUBSTRING(ban1.url_id, '=([0-9]{1,10})')
from banner ban1
where ban1.url_id ~* '.*linkadministration.*[0-9]*';

Both give me the same result!!! The difference being that in case two
the numbers I am catching are at the end of the strings and in case 1
in the middle. Is this normal? Which is correct?
Cheers
Anton

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Anton Melser (#1)
Re: greedy or not? regexps...

"Anton Melser" <melser.anton@gmail.com> writes:

I am trying to understand the function substring.

You haven't actually said anything that sounded surprising; and in any
case, without seeing the data being operated on, we can't comment much
on what's happening. I will note that '=([0-9]*)' is going to match
to the *first* "=" in the string, whether there happen to be any digits
after it or not, because the *-construct can match zero characters.

regards, tom lane

#3Anton Melser
melser.anton@gmail.com
In reply to: Tom Lane (#2)
Re: greedy or not? regexps...

Intellectually challenged Anton strikes again!
I wanted +, not *. Sometimes I think I'm not cut out for IT! :-(
Thanks heaps,
Anton

Show quoted text

On 23/02/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:

"Anton Melser" <melser.anton@gmail.com> writes:

I am trying to understand the function substring.

You haven't actually said anything that sounded surprising; and in any
case, without seeing the data being operated on, we can't comment much
on what's happening. I will note that '=([0-9]*)' is going to match
to the *first* "=" in the string, whether there happen to be any digits
after it or not, because the *-construct can match zero characters.

regards, tom lane