Skip to Main Content
  • Questions
  • Bracket expression in Oracle regular expression

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Saigeetha.

Asked: August 17, 2016 - 4:44 am UTC

Last updated: January 17, 2024 - 10:48 am UTC

Version: Oracle 12c

Viewed 10K+ times! This question is

You Asked

Hello Tom,

I am learning oracle regular expressions. I am using Oracle 12c.
I understand that a bracket expression containing a series of characters returns the first character in the input string.
Thus, select regexp_substr('123 Oracle', '[abc]') from dual
returns the chracter 'a'.
What if bracket expressions are nested?

select regexp_substr('123 Oracle', '[[abc]]') from dual

returns NULL indicating no match. But Why?

How does oracle interpret this regular expression?
Within the outer [] brackets, I expect the inner [] brackets to be treated as literals.
So the output should be the first character among the list [ a b c ] to be matched.
Once again regexp_substr must return 'a'. Why is NULL returned instead?

A variation on this:
select regexp_substr('[]123 Oracle', '[[abc]]') from dual
returns '[]'. And I thought that a bracketed expression would always return a single character.

Any suggestions?

Thanks
Geetha


and Chris said...

As it says in the docs:

A right bracket (]) is treated as a literal if it occurs first in the list.

http://docs.oracle.com/database/121/ADFNS/adfns_regexp.htm#ADFNS242

Emphasis mine. The same applies to the left bracket. So [[abc]] is really matching:

- Any character from [ a b c
- Followed by ]

Which clearly has no match in the string!

To match a, b, c or one of the brackets you could use []abc[] or (\[|\]|[abc]):

select regexp_substr('123 Orac]le', '[[abc]]') r1,
       regexp_substr('123 Oracle', '[]abc[]') r2,
       regexp_substr('123 Oracle', '(\[|\]|[abc])') r3,
       regexp_substr('123[ Oracle', '[]abc[]') r4,
       regexp_substr('123[ Oracle', '(\[|\]|[abc])') r5
from   dual;

R1  R2  R3  R4  R5  
c]  a   a   [   [ 


Complex regular expressions can be difficult to understand. I find the following site invaluable when trying to figure them out:

https://regex101.com/

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

right bracket as literal in regular expression

Rajkumar Rathinam, January 17, 2024 - 6:03 am UTC

Yes, the right bracket will be treated as literal in regular expression only if it is used in first position in the list. Otherwise it won't be treated as literal and will result null value for regexp_substr function.

Example 1 : (if ] is used other than first position)

select regexp_substr('raj k[um]ar','^[a-zA-Z[:space:]\\]\\[]{3,20}$') from dual

Result will be below Error :

ora-12726 unmatched brackets in regular expression

Example 2: (Solution for Example 1)

select regexp_substr('raj k[um]ar','^[]a-zA-Z[:space:]\\[]{3,20}$') from dual ;

Output : raj k[um]ar





Chris Saxon
January 17, 2024 - 10:48 am UTC

Good point