0

I have a table like that:

id: 1
pattern: /test.*

id: 2
pattern: /hello-world

and this input value: /test-request

Now i want to create a query for the given input and postgres should return the row, where the pattern from the second column matches the input (a regex search but the regex is in the field to search)

Is that possible with a postgres database?

1 Answer 1

6

You can use a column value for the regex operator:

select id, pattern 
from patterns
where '/test-request' ~* pattern;

~* does a case-insensitive match, ~ does a case sensitive match.

SQLFiddle example: http://sqlfiddle.com/#!15/388a4/2

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

1 Comment

did not know that it is possible to change the order of the two fields, Thanks!

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.