Search code examples
pythondjangopostgresqldjango-orm

In Django ORM, wildcard \b does not work (Postgres database)


All other wildcards work correctly but for example when I want to use "\bsome\b" pattern, it can't find any result however, there are many rows in the database that have a word "some" inside them.

Other wild cards like ., +, *, \w and ... work properly.

Any idea on what is the problem?

Code:

regex_pattern = r"\bsome\b"
result = tweets.filter(text__regex=regex_pattern)

Solution

  • \b is not the same "boundary" metacharacter in Postgres (SQL) regexps it is in Python regexps; see here for the docs. (It's just backspace, like in regular strings.)

    Instead, you'll probably want \y:

    \y: matches only at the beginning or end of a word