回答編集履歴

1

正規表現に修正

2018/10/04 08:51

投稿

takey
takey

スコア312

test CHANGED
@@ -2,12 +2,92 @@
2
2
 
3
3
  # coding: utf-8
4
4
 
5
- head = "Congratulations @プレイヤー! You caught a level 36 "
5
+ import re
6
6
 
7
- tail = "!"
7
+ pattern = "^.* You caught a level \d?\d? (?P<username>.+)!'$"
8
8
 
9
- str_ = "Congratulations @プレイヤー! You caught a level 36 Mime Jr.!"
10
9
 
10
+
11
+ content = "Congratulations @プレイヤー! You caught a level 36 Mime Jr.!"
12
+
13
+ content = repr(content)
14
+
15
+ m = re.match(pattern, content)
16
+
11
- print(str_[len(head):-len(tail)])
17
+ print(m.group("username"))
18
+
19
+ # Mime Jr.
20
+
21
+
22
+
23
+
24
+
25
+ content = "Congratulations @!!!! You caught a level 36 Mime Jr.!"
26
+
27
+ content = repr(content)
28
+
29
+ m = re.match(pattern, content)
30
+
31
+ print(m.group("username"))
32
+
33
+ # Mime Jr.
34
+
35
+
36
+
37
+
38
+
39
+ content = "Congratulations @You caught a level You caught a level 36 Mime Jr.!"
40
+
41
+ content = repr(content)
42
+
43
+ m = re.match(pattern, content)
44
+
45
+ print(m.group("username"))
46
+
47
+ # Mime Jr.
48
+
49
+
50
+
51
+
52
+
53
+ content = "Congratulations @You caught a level 36 You caught a level 36 Mime Jr.!"
54
+
55
+ content = repr(content)
56
+
57
+ m = re.match(pattern, content)
58
+
59
+ print(m.group("username"))
60
+
61
+ # Mime Jr.
62
+
63
+
64
+
65
+
66
+
67
+ content = "Congratulations @You caught a level 36! You caught a level 36 Mime Jr.!"
68
+
69
+ content = repr(content)
70
+
71
+ m = re.match(pattern, content)
72
+
73
+ print(m.group("username"))
74
+
75
+ # Mime Jr.
76
+
77
+
78
+
79
+
80
+
81
+ content = "Congratulations @You caught a level 36! You caught a level 36 You caught a level 99!"
82
+
83
+ content = repr(content)
84
+
85
+ m = re.match(pattern, content)
86
+
87
+ print(m.group("username"))
88
+
89
+ # You caught a level 99
90
+
91
+
12
92
 
13
93
  ```