djangosnippets.org: Latest snippets tagged with 'regex'https://djangosnippets.org/tags/regex/2013-11-24T14:47:30.132742-06:00regex search in admin forms
2013-11-24T14:47:30.132742-06:00mbeehttps://djangosnippets.org/snippets/3005/<p>Despite warning coming from django developers, I'm still using admin classes to quickly get into reverse engineering databases.</p>
<p>One feature is missing: searching into fields thanks to a regex.</p>
<p>One dirty solution I found is to overwrite get_search_results. But most of the code comes from django itself.</p>
<p>If anyone has …</p>
Freely redistributableSimple templatetag to mark navigation as active using view names or regex
2013-05-23T01:50:49.068525-05:00gluchethttps://djangosnippets.org/snippets/2923/<p>Theses two templatetags make easy to add an active class on a navigation link. The first one is based on a regexp an search if the request path match it. The second one simply use view names.</p>
Freely redistributableUse express.js like url patterns
2012-11-19T08:05:37.969434-06:00ekinertachttps://djangosnippets.org/snippets/2848/<p>Sometimes using regex is a huge pain</p>
Freely redistributableRegEx redirect fallback middleware
2012-07-08T05:23:55.478285-05:00bowdengmhttps://djangosnippets.org/snippets/2784/<p>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.</p>
<p>Useful if you want to add the redirects into the DB - and/or don't have …</p>
Freely redistributablePassword Validation - Require Letters and Numbers - no regex
2011-09-21T12:12:22.258438-05:00watchedmanhttps://djangosnippets.org/snippets/2551/<p>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 …</p>
Freely redistributableReplacing pattern groups by values
2011-08-17T08:26:59.286910-05:00marinhohttps://djangosnippets.org/snippets/2514/<p>This function takes a pattern with groups and replaces them with the given args and/or kwargs. Example:</p>
<p>IMPORTANT: this code is NOT to use replacing Django's reverse function. The example below is just to illustrate how it works.</p>
<p>For a given pattern '/docs/(\d+)/rev/(\w+)/', args=(123,'abc') and kwargs={}, returns '/docs/123/rev/abc/'.</p>
<p>For '/docs/(?P<id>\d+)/rev/(?P<rev>\w+)/', …</p>
Freely redistributableA RegexpField that clean the regex match using the desired format
2010-09-18T23:12:24.744341-05:00nasphttps://djangosnippets.org/snippets/2201/<p>I wanted a way to allow flexible phone number validation while making sure the saved data was uniform.
</p>
<p>ex.
</p>
<p>With:
RegexFormatField(r'^(?(?P<area>d{3}))?[-s.]?(?P<local>d{3})[-s.]?(?P<subscriber>d{4})$',
format='%(area)s %(local)s-%(subscriber)s')
</p>
<p>input:
(444) 444-4444
444 444-4444
444-444-4444
444.444.4444
4444444444
</p>
<p>output:
444 444-4444
</p>
Freely redistributableTemplate filter to convert timecodes into links
2009-09-04T15:16:30.983323-05:00justin_hhttps://djangosnippets.org/snippets/1717/<p>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 …</p>
Freely redistributablematch filter
2009-08-14T08:38:23.757223-05:00gsf0https://djangosnippets.org/snippets/1686/<p>A filter that re.matches a regex against a value. Useful for nav bars as follows:</p>
<pre><code>{% if location.path|match:"/$" %} class="current"{% endif %}
</code></pre>
<p>For <code>location.path</code> see my <a href="/snippets/1685/">location context_processor</a>.</p>
Freely redistributableTemplate tag "ifregex" and "ifnotregex"
2009-05-26T12:04:43.074523-05:00arthurfurlanhttps://djangosnippets.org/snippets/1531/<p>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.</p>
<p>Usage:</p>
<pre><code>{% ifregex "^/comments/" request.path %}
...
{% endifregex %}
{% ifnotregex "^/comments/" request.path %}
...
{% else %}
...
{% endifnotregex %}
</code></pre>
Freely redistributableRegex Comma Number
2008-10-24T18:11:23.448329-05:00petryhttps://djangosnippets.org/snippets/1155/<p>Format Number Based on Regular Expression</p>
<p><strong>Examples</strong></p>
<blockquote>
<p><em>{{.1234|regex_comma_number:'%.4f'}}
</em>'0.1234'</p>
<p><em>{{100|regex_comma_number:'%i'}}
</em>'100'</p>
<p><em>{{ 234.5678|regex_comma_number:'%.4f'}}
</em>'234.5678'</p>
<p><em>{{234.5678|regex_comma_number:'$%.4f'}}
</em>'$234.5678'</p>
<p><em>{{1000|regex_comma_number:'%i'}}
</em>'1,000'</p>
<p><em>{{1234.5678|regex_comma_number:'%.4f'}}
</em>'1,234.5678'</p>
<p><em>{{1234.5678|regex_comma_number:'$%.4f'}}
</em>'$1,234.5678'</p>
<p><em>{{1000000|regex_comma_number:'%i'}}
</em>'1,000,000'</p>
<p><em>{{1234567.5678|regex_comma_number:'%.4f'}}
</em>'1,234,567.5678'</p>
<p><em>{{1234567.5678|regex_comma_number:'$%.4f'}}
</em>'$1,234,567.5678'</p>
<p><em>{{-100|regex_comma_number:'%i'}}
</em>'-100'</p>
<p><em>{{-234.5678|regex_comma_number:'%.4f'}}
</em>-234.5678'</p>
<p><em>{{-234.5678|regex_comma_number:'$%.4f'}}
</em>'$-234.5678'</p>
<p><em>{{-1000|regex_comma_number:'%i'}}
</em>'-1,000'</p>
<p><em>{{-1234.5678|regex_comma_number:'%.4f'}}
</em>'-1,234.5678'</p>
<p><em>{{-1234.5678|regex_comma_number:'$%.4f'}}
</em>'$-1,234.5678'</p>
<p><em>{{-1000000|regex_comma_number:'%i'}}
</em>'-1,000,000'</p>
<p><em>{{-1234567.5678|regex_comma_number:'%.4f'}}
</em>'-1,234,567.5678'</p>
<p><em>{{-1234567.5678|regex_comma_number:'$%.4f'}}
</em>'$-1,234,567.5678'`</p>
</blockquote>
Freely redistributableRegular Expression Dictionary
2007-07-11T23:10:16.097087-05:00skitchhttps://djangosnippets.org/snippets/309/<p>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.</p>
Freely redistributableRegular Expression Replace Template Filter
2007-02-28T22:07:59.753069-06:00joshuahttps://djangosnippets.org/snippets/60/<p>This will perform a regular expression search/replace on a string in your template.</p>
<p><code>{% load replace %}</code>
<code>{{ mystring|replace:"/l(u+)pin/m\1gen" }}</code></p>
<p>If:
<code>mystring = 'lupin, luuuuuupin, and luuuuuuuuuuuuupin are lè pwn'</code> then it will return:
<code>mugen, muuuuuugen, and muuuuuuuuuuuuugen are lè pwn</code></p>
<p>The argument is in the following format:</p>
<pre><code>[delim char]regexp …</code></pre>
Freely redistributable