-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathre_japanese.py
More file actions
executable file
·77 lines (63 loc) · 1.96 KB
/
re_japanese.py
File metadata and controls
executable file
·77 lines (63 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import re
###
# すべて「ひらがな」であればマッチする
###
pattern = u'^[\u3040-\u3098]+$'
# 対象の文字列もUnicodeにあわせる
string = u'あいうえお' # 正常系
result = re.match(pattern, string)
assert result is not None
print(result)
# <_sre.SRE_Match object at ...>が出力される
string = u'XあいうえおX' # 異常系、前後に対象外文字あり
result = re.match(pattern, string)
assert result is None
print(result)
# Noneが出力される
string = u'あいXうえお' # 異常系、途中に対象外文字あり
result = re.match(pattern, string)
assert result is None
print(result)
# Noneが出力される
###
# すべて「カタカナ」であればマッチする
###
pattern = u'^[\u30a1-\u30fa\u30fc]+$'
# 対象の文字列もUnicodeにあわせる
string = u'アイウエオ' # 正常系
result = re.match(pattern, string)
assert result is not None
print(result)
# <_sre.SRE_Match object at ...>が出力される
string = u'XアイウエオX' # 異常系、前後に対象外文字あり
result = re.match(pattern, string)
assert result is None
print(result)
# Noneが出力される
string = u'アイXウエオ' # 異常系、途中に対象外文字あり
result = re.match(pattern, string)
assert result is None
print(result)
# Noneが出力される
###
# すべて「漢字」であればマッチする
###
pattern = u'^[\u4e00-\u9fff]+$'
# 対象の文字列もUnicodeにあわせる
string = u'文字列' # 正常系
result = re.match(pattern, string)
assert result is not None
print(result)
# <_sre.SRE_Match object at ...>が出力される
string = u'X文字列X' # 異常系、前後に対象外文字あり
result = re.match(pattern, string)
assert result is None
print(result)
# Noneが出力される
string = u'文字X列' # 異常系、途中に対象外文字あり
result = re.match(pattern, string)
assert result is None
print(result)
# Noneが出力される