cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Do regular expressions support backreferences?

VolkerBarth
Contributor
0 Likes
2,290

When using groups within a regex, it is usually possible to "back reference" to such a group via its number (or name), such as "\\1" to reference to the first (capturing) group.

Am I right that this is not supported with SQL Anywhere?

Here's a sample:

-- search for a number followed by non-digits and a further number
select regexp_substr('abc-101-abd-102-abe', '(\\\\d+)\\\\D+\\\\d+');
-- -> returns "101-abd-102"

-- If I want the match to restrict to those cases
-- where the first and second number are identical,
-- that requires a backreference via \\1, such as
select regexp_substr('abc-101-abd-102-abe', '(\\\\d+)\\\\D+\\\\1'); -- should not match
select regexp_substr('abc-101-abd-101-abe', '(\\\\d+)\\\\D+\\\\1'); -- should match

However, adding '\\\\1' to the regexp pattern leads to SQLCODE -1135 aka "Invalid regular expression: unsupported syntax in '(\\d+)\\D+\\1'"

I'm using 16.0.0.2704 but I guess the regex support has been unchanged for quite a number of versions.

Accepted Solutions (1)

Accepted Solutions (1)

ian_mchardy
Product and Topic Expert
Product and Topic Expert

Hi Volker,

SQL Anywhere does NOT support back references in regular expressions.

See https://help.sap.com/viewer/40c01c3500744c85a02db71276495de5/17.0/en-US/81735cab6ce21014b8a287b4fbb8... for the list of escaped characters supported, and note that back references are not listed.

Ian

VolkerBarth
Contributor
0 Likes

Thanks for the confirmation. I noticed that list but was not sure as the docs also state:

For REGEXP and REGEXP_SUBSTR, regular expression syntax and support is consistent with Perl 5.

which includes backreferences AFAIK.

Breck_Carter
Participant
0 Likes

. . . cannot . . . resist . . . must . . . post . . . this 🙂

VolkerBarth
Contributor
0 Likes

Particularly funny as I came across that one by chance (?) yesterday, too:)

Answers (0)