0

I have a list of paths in Postgres:

/approve/optimal/roadmap/

/approve/optimal/roadmap/payments/

/approve/optimal/roadmap/payments/benefits/

/approve/optimal/roadmap/payments/healthcare/

I'd like a regex that will get the direct descendent of /approve/optimal/roadmap/ (ie: /approve/optimal/roadmap/payments/, but nothing else)

I have looked at the ~ operator, but can't seem to make it work with POSIX regex.

SELECT * FROM table WHERE path ~ '/approve/optimal/roadmap/.+?/^.+/'

If this is possible without regex, I'd love to implement THAT solution, as regex just gives me a headache.

2
  • Can you clarify, do you want to GET the value e.g. "payments", or do you want to get records WHERE path ~ ... ? Commented Apr 6, 2018 at 15:11
  • I would like the query to return records with the full path of direct descendants. Commented Apr 6, 2018 at 15:19

1 Answer 1

2

You can use the following :

SELECT * FROM table WHERE path ~ '^/approve/optimal/roadmap/[^/]+/$'

Try it here

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

1 Comment

Thank you, this is exactly what I needed. Saved me from ingesting more Advil.

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.