Skip to main content

Fields and utilities to work with regular expression in Django

Project description

https://badge.fury.io/py/django-regex.png https://travis-ci.org/saxix/django-regex.png?branch=master https://pypip.in/d/django-regex/badge.png

Fields and utilities to work with regular expression in Django

The Django regex field provides custom fields and utilities for a Django model that stores a regex. This provides the ability to easily store regex patterns and access them as compiled regular expressions from your models.

Patterns can be expressed in perl syntax to set regex flags.

Components

RegexField

Django fields to store regular expressions

class DemoModel(models.Model):
    regex = RegexField(flags=re.I)


o = DemoModel.objects.create(regex='^1$')
o.regex.match('1')

RegexFlagsField

As RegexField but allows to set compilation flags (see: https://docs.python.org/2/howto/regex.html#compilation-flags) It is rendered with proper widget

from django_regex.validators import compress
import re

class DemoModel(models.Model):
    regex = RegexFlagsField()

o = DemoModel.objects.create(regex=compress(['aa', re.I]))
o.regex.match('AA')

o = DemoModel.objects.create(regex=compress(['aa', 'i'])) # use human shortcuts
o.regex.match('AA')

RegexFlagsField stores pattern and flags in the same db column as string in the format <regex.pattern><separator><regex.flags>

separator is chr(0) can be customized using settings DJANGO_REGEX_SEPARATOR or per each field using flags_separator argument.

from django_regex.validators import compress
import re

class DemoModel(models.Model):
    regex = RegexFlagsField(flags_separator='/')

o = DemoModel.objects.create(regex='aa/i')
o.regex.match('AA')

RegexList

list that matches content against valid regular expressions

rules = RegexList(['\d*'])
1 in rules  # True
'1' in rules  # True
'a' in rules  # False

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-regex-0.4.1.tar.gz (25.6 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page