Login

Tag "regex"

Snippet List

regex search in admin forms

Despite warning coming from django developers, I'm still using admin classes to quickly get into reverse engineering databases. One feature is missing: searching into fields thanks to a regex. One dirty solution I found is to overwrite get_search_results. But most of the code comes from django itself. If anyone has a better idea ;) **Usage:** 1. works since get_search_results is part of ModelAdmin (1.5 if I remember well) 2. Inherit your Admin class from RegexModelAdmin 3. enclose by / the field you want to regex with: `search_fields = ['/field/', ]`

  • admin
  • search
  • regex
  • field
Read More

RegEx redirect fallback middleware

Simple middleware to complement the built in redirect middleware app. Add this after the contrib.redirect middleware - this will be fired if a 404 is triggered and the contrib.redirect fails to find a suitable redirect. Useful if you want to add the redirects into the DB - and/or don't have access to the .htaccess script or whatever HTTP server based redirect machinery your site runs off. You simply add in regex 'old_path' and 'new_path' entries into the same redirect app, but this will try to do a regex find and replace - i.e. >>> r = new Redirect() >>> r.old_path = '/my/test/path/([a-z]+)/with/regex/' >>> r.new_path = '/my/result/path/$1/with/regex/' >>> r.save() this will convert: /my/test/path/abcdef/with/regex/ into: /my/result/path/abcdef/with/regex/' also works with query strings: /index.php?section=products >>> old_path = '/index.php/\?section=([a-z]+)' #need to add in the forward slash if ADD_SLASHES = True in your settings, and escape the question mark. >>> new_path = '/section/$1/' converts the url to '/section/products/'

  • regex
  • Redirect
  • /
Read More

Password Validation - Require Letters and Numbers - no regex

Simple password validation for user registration - requires that password be 7 or more characters and contain both letters and numbers. Original validation with regex approach developed by kurtis. Optimized no-regex version based on code from watchedman ran as fast or significantly faster on all systems on which we tested it.

  • registration
  • model
  • regex
  • user
  • validation
  • form
  • password
Read More

Replacing pattern groups by values

This function takes a pattern with groups and replaces them with the given args and/or kwargs. Example: IMPORTANT: this code is NOT to use replacing Django's reverse function. The example below is just to illustrate how it works. For a given pattern '/docs/(\d+)/rev/(\w+)/', args=(123,'abc') and kwargs={}, returns '/docs/123/rev/abc/'. For '/docs/(?P<id>\d+)/rev/(?P<rev>\w+)/', args=() and kwargs={'rev':'abc', 'id':123}, returns '/docs/123/rev/abc/' as well. When both args and kwargs are given, raises a ValueError.

  • regex
  • reverse
Read More

A RegexpField that clean the regex match using the desired format

I wanted a way to allow flexible phone number validation while making sure the saved data was uniform. ex. With: RegexFormatField(r'^\(?(?P<area>\d{3})\)?[-\s.]?(?P<local>\d{3})[-\s.]?(?P<subscriber>\d{4})$', format='%(area)s %(local)s-%(subscriber)s') input: (444) 444-4444 444 444-4444 444-444-4444 444.444.4444 4444444444 output: 444 444-4444

  • regex
  • format
  • field
Read More

Template filter to convert timecodes into links

This template filter, "jumptime" will find any timecodes in a chunk of text and convert them to links that can be used to jump a video player to that point. E.g., If there is the string "3:05", it will be converted into a link that can be used to jump to that point. This is similar to what youtube does. For information on how to implement, see Django's [custom template tag information](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags). You'd use this with some javascript like this: `jQuery(document).ready(function(){ jQuery('a.jumpToTime').bind('click',function(){ player.sendEvent('PLAY'); player.sendEvent('SEEK', jQuery(this).attr('value')); }); });`

  • regex
  • time
  • re
  • timecode
Read More

match filter

A filter that re.matches a regex against a value. Useful for nav bars as follows: {% if location.path|match:"/$" %} class="current"{% endif %} For `location.path` see my [location context_processor](/snippets/1685/).

  • regex
  • match
  • nav
Read More

Template tag "ifregex" and "ifnotregex"

Outputs the contents of the block if the second argument matches (or not, depending on the tag) the regular expression represented by the first argument. Usage: {% ifregex "^/comments/" request.path %} ... {% endifregex %} {% ifnotregex "^/comments/" request.path %} ... {% else %} ... {% endifnotregex %}

  • template
  • templatetag
  • regex
  • ifregex
Read More

Regex Comma Number

Format Number Based on Regular Expression **Examples** >*{{.1234|regex_comma_number:'%.4f'}} >*'0.1234' >*{{100|regex_comma_number:'%i'}} >*'100' >*{{ 234.5678|regex_comma_number:'%.4f'}} >*'234.5678' >*{{234.5678|regex_comma_number:'$%.4f'}} >*'$234.5678' >*{{1000|regex_comma_number:'%i'}} >*'1,000' >*{{1234.5678|regex_comma_number:'%.4f'}} >*'1,234.5678' >*{{1234.5678|regex_comma_number:'$%.4f'}} >*'$1,234.5678' >*{{1000000|regex_comma_number:'%i'}} >*'1,000,000' >*{{1234567.5678|regex_comma_number:'%.4f'}} >*'1,234,567.5678' >*{{1234567.5678|regex_comma_number:'$%.4f'}} >*'$1,234,567.5678' >*{{-100|regex_comma_number:'%i'}} >*'-100' >*{{-234.5678|regex_comma_number:'%.4f'}} >*-234.5678' >*{{-234.5678|regex_comma_number:'$%.4f'}} >*'$-234.5678' >*{{-1000|regex_comma_number:'%i'}} >*'-1,000' >*{{-1234.5678|regex_comma_number:'%.4f'}} >*'-1,234.5678' >*{{-1234.5678|regex_comma_number:'$%.4f'}} >*'$-1,234.5678' >*{{-1000000|regex_comma_number:'%i'}} >*'-1,000,000' >*{{-1234567.5678|regex_comma_number:'%.4f'}} >*'-1,234,567.5678' >*{{-1234567.5678|regex_comma_number:'$%.4f'}} >*'$-1,234,567.5678'`

  • templatetag
  • regex
  • format
  • comma
  • number
Read More

Regular Expression Dictionary

Cute little dictionary-like object that stores keys as regexs and looks up items using regex matches. It actually came in handy for a project where keys were regexes on URLs.

  • regex
  • re
  • dict
Read More

Regular Expression Replace Template Filter

This will perform a regular expression search/replace on a string in your template. `{% load replace %}` `{{ mystring|replace:"/l(u+)pin/m\1gen" }}` If: `mystring = 'lupin, luuuuuupin, and luuuuuuuuuuuuupin are lè pwn'` then it will return: `mugen, muuuuuugen, and muuuuuuuuuuuuugen are lè pwn` The argument is in the following format: [delim char]regexp search[delim char]regexp replace

  • regex
  • regular-expressions
  • templates
  • filters
Read More

13 snippets posted so far.