1

Is there a solution in postgres for matching as per below example:

'test' table entries:

id |                            url                             
----+-------------------------------------------------------------
 1  | /services/system
 2  | /services/system/{uuid}/group
 3  | /services/system/{uuid}/group/{uuid}/port

I want to match the following input strings against the url column that exist in table:

1. /services/system/1/group          --> should match row 3
2. /services/system/1/group/2/port   --> should match row 3
3. /services/system/1/group          --> should match row 2
4. /services/system/1/group/2        --> should not match
5. /services/system/1                --> should not match

I tried following query to match the match the 3rd row, but it did not work:

select * from test where regexp_replace(url, '{uuid}', '*', 'g') ~ '/services/system/1/group/1/port'

Any solution?

4
  • * isn't the wildcard character in a regex. You need to use .+ or maybe [0-9]+ to only match numbers Commented Jan 13, 2021 at 7:37
  • okay but '.+' didn't work either. Also the uuid placeholder may not necessarily be number. /1/ and /2/ are just used to simplify the example. Commented Jan 13, 2021 at 7:50
  • 1
    I think you should try like this.. select * from test where regexp_replace('/services/system/1/group/1/port', '[0-9]+', '{uuid}', 'g') = url Commented Jan 13, 2021 at 7:51
  • @AkhileshMishra As I said, {uuid} is not a number. Also the input string is dynamic in nature, so this kind of matching isn't a generic solution Commented Jan 13, 2021 at 7:59

1 Answer 1

2

Considering your scenarios given in question and comments you can use below query:

select * from test 
where '/services/system/1/group' ~ concat('^',replace(url,'{uuid}','[^/]+'),'$')

Here it will check for any character except / in place of {uuid}

Edit If you want only alphanumeric then you should use try this:

select * from test 
where '/services/system/1/group' ~ concat('^',replace(url,'{uuid}','[\w]+'),'$')

DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

This query works! Thanks for the solution.
This query matches only when {uuid} is single digit. For e.g: it does not match id=3 for the following query in your demo example: select * from test where '/services/system/1001/group/2001/port' ~concat('^',replace(url,'{uuid}','[^/]'),'$')
shouldn't replacing '{uuid}' with '[\w+]' had worked? my {uuid} is alphanumeric.
downvoting as this is not a generic solution.
actually I missed + in it. it should be [^/]+ . If you want to restrict to alphanumeric then you can use [\w]+. But if it is a statdard uuid then it should be '[\w-]+`. I have updated the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.