dengar81
dengar81

Reputation: 2525

Django, MySQL and regex: not supporting re syntax?

A complicated syntax like:

Model.objects.filter(url__iregex=r'^\S+\/\d+\/\S+$')

does not work. But something where I leave parts of the string that should be escaped, unescaped does returns elements:

Model.objects.filter(url__iregex=r'^http://www.c')

What am I doing wrong, or is regex not supported with MySQL in Django?

Upvotes: 1

Views: 518

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627302

You need to use the MySQL REGEXP syntax for the expression:

^[^[:space:]]+/[0-9]+/[^[:space:]]+$

where

  • ^ - start of string
  • [^[:space:]]+ - 1 or more characters other than whitespace
  • / - a / symbol
  • [0-9]+ - 1 or more digits
  • / - a /
  • [^[:space:]]+ - 1 or more characters other than whitespace
  • $ - end of string.

More details on REGEXP syntax

Upvotes: 2

Related Questions