Using capturing groups to demonstrate the order of evaluation, the regex H|ha+
is equivalent to the following:
(H|(h(a+)))
This is because the precedence rules (as seen below) are applied in order from the highest precedence (the lowest numbered) one to the lowest precedence (the highest numbered) one:
Rule 5 → (a+)
The +
is grouped with the a
because this operator works on the preceding single character, back-reference, group (a "marked sub-expression" in Oracle parlance), or bracket expression (character class).
Rule 6 → (h(a+))
The h
is then concatenated with the group in the preceding step.
Rule 8 → (H|(h(a+)))
The H
is then alternated with the group in the preceding step.
Precedence table from section 9.4.8 of the POSIX docs for regular expressions (there doesn't seem to be an official Oracle table):
+---+----------------------------------------------------------+
| | ERE Precedence (from high to low) |
+---+----------------------------------------------------------+
| 1 | Collation-related bracket symbols | [==] [::] [..] |
| 2 | Escaped characters | \<special character> |
| 3 | Bracket expression | [] |
| 4 | Grouping | () |
| 5 | Single-character-ERE duplication | * + ? {m,n} |
| 6 | Concatenation | |
| 7 | Anchoring | ^ $ |
| 8 | Alternation | | |
+---+-----------------------------------+----------------------+
The table above is for Extended Regular Expressions. For Basic Regular Expressions see 9.3.7.