Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for Postgres

I want to pull a particular field from a postgres table that conforms to this pattern:

/^Untitled Deal \d+$/

For example:

Untitled Deal 1

Untitled Deal 2

Untitled Deal 3

I have the query in postgres which is not working:

SELECT "name" FROM "deals" WHERE ("name" ILIKE '/^Untitled Deal \\d+$/');

Can anyone point out what I am doing wrong?

like image 955
dagda1 Avatar asked Sep 06 '25 03:09

dagda1


2 Answers

You need to use ~* instead of ILIKE, if you want to pattern match against POSIX-style regular expressions.

I.e.:

SELECT "name" FROM "deals" WHERE ("name" ~* E'^Untitled Deal \\d+$');

See also:

  • PostgreSQL documentation on pattern matching
like image 65
Sebastian Paaske Tørholm Avatar answered Sep 07 '25 19:09

Sebastian Paaske Tørholm


you simply can use LIKE and %

ie.,

SELECT name FROM deals WHERE name LIKE 'Untitled Deal %'
like image 23
Mani Deep Avatar answered Sep 07 '25 19:09

Mani Deep