Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator precedence in regular expressions

What is the default operator precedence in Oracle's regular expressions when they don't contain parentheses?

For example, given

 H|ha+

would it be evaluated as H|h and then concatenated to a as in ((H|h)a), or would the H be alternated with ha as in (H|(ha))?

Also, when does the + kick in, etc.?

like image 325
Kenny Avatar asked Apr 26 '16 16:04

Kenny


People also ask

Which one of the following is the precedence of operations in regular expression in descending order?

(i) The regular expression operators have the following order of precedence (in decreasing order): star, concatenation, union.

How many operators are used in regular languages?

A regular expression describes a language using three operations. A regular expression (RE) describes a language. It uses the three regular operations. These are called union/or, concatenation and star.

Which are 3 uses of regular expression?

Regular expressions are useful in any scenario that benefits from full or partial pattern matches on strings. These are some common use cases: verify the structure of strings. extract substrings form structured strings.


1 Answers

Given the Oracle doc:

Table 4-2 lists the list of metacharacters supported for use in regular expressions passed to SQL regular expression functions and conditions. These metacharacters conform to the POSIX standard; any differences in behavior from the standard are noted in the "Description" column.

And taking a look at the | value in that table:

The expression a|b matches character a or character b.

Plus taking a look at the POSIX doc:

Operator precedence The order of precedence for of operators is as follows:

  1. Collation-related bracket symbols [==] [::] [..]

  2. Escaped characters \

  3. Character set (bracket expression) []

  4. Grouping ()

  5. Single-character-ERE duplication * + ? {m,n}

  6. Concatenation

  7. Anchoring ^$

  8. Alternation |

I would say that H|ha+ would be the same as (?:H|ha+).

like image 145
Thomas Ayoub Avatar answered Oct 08 '22 03:10

Thomas Ayoub