-
-
Save n37r06u3/a7afada9a8500777c4babfd48bfeaf82 to your computer and use it in GitHub Desktop.
djangoのRegexFieldの使用例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import re | |
from django import forms | |
USERID_REGEX = re.compile(r"^[\w-]+$") | |
class UserForm(forms.Form): | |
userid = forms.RegexField(max_length=64, regex=USERID_REGEX) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import re | |
def main(): | |
test_str = u"まだまだだね。" | |
if re.search(ur"^(まだ)\1だね。?$", test_str): | |
# マッチするときはMatchObjectを返し、しないときはNoneを返す。 | |
# if文でNoneは偽に評価されるからこれで文字列がプリントされる。 | |
print test_str | |
if __name__ == "__main__": | |
main() #=> まだまだだね。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import unittest | |
import string | |
from forms import UserForm | |
VALID_STR = string.ascii_lowercase | |
VALID_STR += string.ascii_uppercase | |
VALID_STR += string.digits | |
VALID_STR += "-_" | |
class UserFormTest(unittest.TestCase): | |
def assert_match(self, userid, expect): | |
form = UserForm(dict(userid=userid)) | |
self.assertEqual(form.is_valid(), expect) | |
def test_valid(self): | |
"""許容文字だけで構成される文字列がエラーにならないことを検証する。 """ | |
self.assert_match(VALID_STR, True) | |
def test_invalid(self): | |
"""許容されない文字が含まれる場合にエラーになることを検証する。""" | |
self.assert_match("life & food", False) | |
if __name__ == "__main__": | |
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import re | |
import unittest | |
import string | |
VALID_STR = string.ascii_lowercase | |
VALID_STR += string.ascii_uppercase | |
VALID_STR += string.digits | |
VALID_STR += "-_" | |
class RegexTest(unittest.TestCase): | |
"""半角英数字、半角ハイフンアンダースコアだけで構成される文字列であるか調べる正規表現のテスト""" | |
def test_regex1(self): | |
"""許容文字だけで文字列が構成される というアプローチのパターン。 """ | |
regex = re.compile(r"^[\w-]*$") | |
self.assertTrue(regex.search(VALID_STR)) | |
self.assertFalse(regex.search(u"今川義元")) | |
def test_regex2(self): | |
"""許容文字以外が含まれるかどうか判定する というアプローチのパターン。 """ | |
regex = re.compile(r"[^\w-]") | |
self.assertFalse(regex.search(VALID_STR)) | |
self.assertTrue(regex.search(u"今川義元")) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment