'MySQL 8.0 regexp_replace with parentheses (callback)
I have a table that currently contains a log of urls used in API calls, for example:
https : // mysite.org/data/2.5/endpoint?lat=32.2049&lon=-95.8555
(The spaces are in that URL due to stackoverflows insistence.)
The challenge is to now separate these into api_server, api_endpoint and api_parameters. It's pretty easy to get the api_server and api_parms from this, since it's simple removal of what doesn't belong from one end. But I'm having a challenge when it comes to callback.
My experience with regexp, (I'm by far not a newbie) has been that there's inconsistency with regards to need for escaping parentheses for a callback - and in the case of mySQL, just how to do that escaping. I've tried several different ways of approaching this, my three most likely candidates being:
mysql> with t1 as (select 'https : // mysite.org/data/2.5/endpoint?lat=32.2049&lon=-95.8555' as url)
-> select regexp_replace(url, '[^/]*$', '') as api_server
-> , regexp_replace(url, '^[^?]*\\?', '?') as api_parms
-> , regexp_replace(url, '^[^/?]*/([:alnum:]*)\?.*$', '\\1') AS endpoint
-> FROM t1;
+------------------------------+---------------------------+----------+
| api_server | api_parms | endpoint |
+------------------------------+---------------------------+----------+
| https : // mysite.org/data/2.5/ | ?lat=32.2049&lon=-95.8555 | 1 |
+------------------------------+---------------------------+----------+
1 row in set (0.14 sec)
mysql> with t1 as (select 'https : // mysite.org/data/2.5/endpoint?lat=32.2049&lon=-95.8555' as url)
-> select regexp_replace(url, '[^/]*$', '') as api_server
-> , regexp_replace(url, '^[^?]*\\?', '?') as api_parms
-> , regexp_replace(url, '^[^/?]*/\\([:alnum:]*\\)\?.*$', '\\1') AS endpoint
-> FROM t1;
+------------------------------+---------------------------+---------------------------------------------------------------+
| api_server | api_parms | endpoint |
+------------------------------+---------------------------+---------------------------------------------------------------+
| https : // mysite.org/data/2.5/ | ?lat=32.2049&lon=-95.8555 | https : // mysite.org/data/2.5/endpoint?lat=32.2049&lon=-95.8555 |
+------------------------------+---------------------------+---------------------------------------------------------------+
1 row in set (0.03 sec)
mysql> with t1 as (select 'https : // mysite.org/data/2.5/endpoint?lat=32.2049&lon=-95.8555' as url)
-> select regexp_replace(url, '[^/]*$', '') as api_server
-> , regexp_replace(url, '^[^?]*\\?', '?') as api_parms
-> , regexp_replace(url, '^[^/?]*/\([:alnum:]*\)\?.*$', '\\1') AS endpoint
-> FROM t1;
+------------------------------+---------------------------+----------+
| api_server | api_parms | endpoint |
+------------------------------+---------------------------+----------+
| https : // mysite.org/data/2.5/ | ?lat=32.2049&lon=-95.8555 | 1 |
+------------------------------+---------------------------+----------+
1 row in set (0.04 sec)
(The difference being in how (and whether) I escape the callback parentheses.)
The mySQL documentation on this, sadly is extremely poor... they briefly mention the parentheses in passing, saying that they have to be escaped with double-backslash, but as far as the callback itself, I found nothing.
Is it even possible to use callback in regexp_replace? And if so, what's the trick I'm missing? And if not, why did they go through all the trouble of implementing and then explaining the callback escape requirement?
Thanks for your consideration and time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
