Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Regular Expressions: Greedy vs Non-Greedy

572471Jul 3 2007 — edited Jul 3 2007
Guys, I just can't explain and find any explanation in the doc for such a behaviour:
SQL> with t as (select 'the 1 january of the year 2007' str from dual)
  2  select regexp_substr(str,'.*?[[:digit:]][ ][[:alpha:]]+.*') substr1,
  3         regexp_substr(str,'.*?[[:digit:]][ ][[:alpha:]]+.*$') substr2
  4  from t
  5  /

SUBSTR1       SUBSTR2
------------- ------------------------------
the 1 january the 1 january of the year 2007

SQL
the first part of a pattern is '.*?' - non-greedy seacrh for combination of any symbols.
It is followed by 1 digit, then 1 space then a consequent greedy combination of alpha characters
an the last part of the mask is '.*' in the first case and '.*$' in the second.
The only difference in '$' in the end.

AFAIK '.*' in the first case should stand for GREEDY search of a combination of any symbols.
So in my opinion if '.*' stands in the end of the mask it should be equivalent to '.*$',
but somehow it becomes NON-GREEDY.
I just can't explain why.
Can anyone help?

Thanks.

PS
SQL> select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE	10.2.0.1.0	Production

TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production

SQL
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 31 2007
Added on Jul 3 2007
7 comments
2,346 views