--(Greediness) this is from oracle regular expressions pocket reference book.
In this example:
select regexp_substr('In the beginning','.+[[:space:]]') from dual;
Output: In the
This example show greediness of oracle expression as regexp engine will indeed want to find more, it looks to see whether it can match an even longer run of characters.
but in second example:
select regexp_substr('bbb',b|bb') from dual;
Output: b
why it doesn't show its first behavior here? Could you please explain this?
It was written oracle ignores the "longest match possible" rule because the overhead of computing all possible permutations and determining which is the longest can be excessive but then why it is calculating longest match possible in first example?
The explanation that "was written" (from your last paragraph) is bs (a technical term, don't worry if you are not familiar with it).
Greediness refers to matching ONE match pattern against the base string. In the second example, there are two match patterns, 'b' and 'bb', and they are given as ALTERNATIVES: match either the first pattern or the second. Both match patterns are completely deterministic, there is no "greediness" attached to either one of them. In the processing, the search will stop as soon as either the first or the second "match pattern" is found in the input string. In this case 'b' is matched first, so the search ends. (And, if you are curious, the FIRST b in 'bbb' is the one matched and returned.)
In a search with alternatives, first the input string is matched against the FIRST alternative, in all possible ways. Only if no match is found anywhere in the input string, the second alternative is tried. See for example (and notice greediness at work):
SQL> select regexp_substr('bbb', 'b+|bb') as res from dual;
RES
---
bbb
EDIT
However, if you have greedy matching only in the SECOND alternative, but the FIRST alternative finds a match, greediness never comes into play. The point is that "FIRST before SECOND in an alternation" has higher priority than "greediness".
SQL> select regexp_substr('bbb', 'b|b*') as res from dual;
RES
---
b