回答編集履歴

3

コードが重複していた

2018/08/13 15:44

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -11,28 +11,6 @@
11
11
 
12
12
 
13
13
  ```python
14
-
15
- def replacement(text):
16
-
17
- replacement_text = re.sub(r'[command((.+))]', replacement_word, text)
18
-
19
- return replacement_text
20
-
21
-
22
-
23
- def replacement_word(command):
24
-
25
- command_name = command.group(1)
26
-
27
- if command_name == 'str1':
28
-
29
- return 'commandは' + command_name + 'です'
30
-
31
- else:
32
-
33
- return 'そのほかのコマンドです'
34
-
35
-
36
14
 
37
15
  import re
38
16
 

2

動作例を追加

2018/08/13 15:44

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -32,6 +32,52 @@
32
32
 
33
33
  return 'そのほかのコマンドです'
34
34
 
35
+
36
+
37
+ import re
38
+
39
+ def replacement(text):
40
+
41
+ replacement_text = re.sub(r'[command((.+))]', replacement_word, text)
42
+
43
+ return replacement_text
44
+
45
+
46
+
47
+ def replacement_word(command):
48
+
49
+ command_name = command.group(1)
50
+
51
+ print(command_name)
52
+
53
+ if command_name == 'str1':
54
+
55
+ return 'commandは' + command_name + 'です'
56
+
57
+ else:
58
+
59
+ return 'そのほかのコマンドです'
60
+
61
+
62
+
63
+ print(replacement("[command(str1)]"))
64
+
65
+ print(replacement("[command(hoge)]"))
66
+
67
+ """ =>
68
+
69
+ str1
70
+
71
+ commandはstr1です
72
+
73
+ hoge
74
+
75
+ そのほかのコマンドです
76
+
77
+ """
78
+
79
+
80
+
35
81
  ```
36
82
 
37
83
  マッチオブジェクトの細かい仕様などはドキュメントで確認してください。

1

groupに引数が必要

2018/08/13 15:01

投稿

hayataka2049
hayataka2049

スコア30933

test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  def replacement_word(command):
24
24
 
25
- command_name = command.group()
25
+ command_name = command.group(1)
26
26
 
27
27
  if command_name == 'str1':
28
28