I'm trying to extract an email from Basic Auth string like this:
SELECT substring(decode(substring('"authorization":["Basic dGVzdEB0ZXN0LmNvbTpwYXNzd29yZA=="]', '(?<="Basic ).*?(?="])'), 'base64'), '^.*(?=:)');
Expected result is [email protected], however I'm getting an error instead:
[22P02] ERROR: invalid input syntax for integer: "^.*(?=:)"
What's the problem here? Does postgres think that substring second argument must be an integer for some reason?
EDIT: Simplified this query a little just to demo that decoding is working:
SELECT decode(substring('"authorization":["Basic dGVzdEB0ZXN0LmNvbTpwYXNzd29yZA=="]', '(?<="Basic ).*?(?="])'), 'base64');
result: [email protected]:password
EDIT2: To people pointing out the absence of from - according to postgres docs you don't need it and it's interchangeable with a comma in this case - https://www.postgresql.org/docs/10/static/functions-matching.html
Example from this page:
SELECT SUBSTRING('XY1234Z', 'Y*([0-9]{1,3})');
Result: 123
EDIT3: Spelling.
substring()function also takes regural expression as an argument.