yevg
yevg

Reputation: 1966

Making a SQL query via regex matching

Is it possible to make a SQL query by matching a pattern? I know SQL allows for wildcards but I dont think they fit my use case.

Suppose I have a table that contains the following record (represented here in JSON):

Table Name: url_stuff

{
    id: 2
    path: \/user\/(.*)\/
    value: "I am the user path"
}

Then suppose I had the following string representing a URL path:

/user/gandalfthewhite

I would like to make a query that returns this record.

SELECT * FROM url_stuff WHERE path LIKE '/user/gandalfthewhite'

Obviously this wont work, but perhaps there is some other way to use SQL such that /user/gandalfthewhite matches \/user\/(.*)\/ as it would with regex and return the above record.

One solution is obviously to grab all records from the database and search via regex after the fact, but this would not be scaleable for a large number of records. I would ideally be able to grab all matching records with a query directly.

Upvotes: 0

Views: 41

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

If I understand correctly, you can just use regexp:

SELECT *
FROM url_stuff
WHERE '/user/gandalfthewhite' REGEXP url

Upvotes: 1

Related Questions