回答編集履歴

7

プログラム変更

2020/01/24 04:12

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -1,43 +1,45 @@
1
1
  ```python
2
2
 
3
- >>> import re
3
+ import re
4
4
 
5
- >>> text = 'line1\nline2\nline3\nline4\n'
6
5
 
7
- >>> text.split('\n', 2)
8
6
 
9
- ['line1', 'line2', 'line3\nline4\n']
7
+ def number_in_second_line(text):
10
8
 
11
- >>> if '\n' in text and re.search(r'\d', text.split('\n', 2)[1]):
9
+ match = re.match("[^\n]*\n[^\n]*([0-9]+)", text)
12
10
 
13
- ... print('2行目があり、かつ、2行目に数字が含まれています')
11
+ return match and match[1] or None
14
12
 
15
- ... else:
16
13
 
17
- ... print('2行目がない、または、2行目に数字が含まれていません')
18
14
 
19
- ...
15
+ print(number_in_second_line("line1"))
20
16
 
21
- 2行目があり、かつ、2行目に数字が含まれています
17
+ print(number_in_second_line("line1\nline2"))
18
+
19
+ print(number_in_second_line("line1\nline2\nline3\n"))
20
+
21
+ print(number_in_second_line("line1\nlinex"))
22
+
23
+ print(number_in_second_line("line1\nlinex\nline3\n"))
22
24
 
23
25
  ```
24
26
 
25
27
 
26
28
 
27
- ```python
29
+ 実行結果
28
30
 
29
- >>> re.search(r'\d+', '全角の123')[0]
30
31
 
31
- '123'
32
-
33
- >>> re.search(r'\d+', '半角の123')[0]
34
-
35
- '123'
36
-
37
- >>> re.search('[0-9]+', '全角の123')
38
-
39
- >>> re.search('[0-9]+', '半角の123')[0]
40
-
41
- '123'
42
32
 
43
33
  ```
34
+
35
+ None
36
+
37
+ 2
38
+
39
+ 2
40
+
41
+ None
42
+
43
+ None
44
+
45
+ ```

6

空白挿入

2020/01/24 04:12

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  >>> text = 'line1\nline2\nline3\nline4\n'
6
6
 
7
- >>> text.split('\n',2)
7
+ >>> text.split('\n', 2)
8
8
 
9
9
  ['line1', 'line2', 'line3\nline4\n']
10
10
 

5

例追記

2020/01/23 21:06

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -30,9 +30,13 @@
30
30
 
31
31
  '123'
32
32
 
33
- >>> re.search(r'[0-9]+', '角の123')
33
+ >>> re.search(r'\d+', '角の123')[0]
34
34
 
35
+ '123'
36
+
37
+ >>> re.search('[0-9]+', '全角の123')
38
+
35
- >>> re.search(r'[0-9]+', '半角の123')[0]
39
+ >>> re.search('[0-9]+', '半角の123')[0]
36
40
 
37
41
  '123'
38
42
 

4

複数数字に変更

2020/01/23 20:18

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -26,14 +26,14 @@
26
26
 
27
27
  ```python
28
28
 
29
- >>> re.search(r'\d+', '全角の1')[0]
29
+ >>> re.search(r'\d+', '全角の123')[0]
30
30
 
31
- '1'
31
+ '123'
32
32
 
33
- >>> re.search(r'[0-9]', '全角の1')
33
+ >>> re.search(r'[0-9]+', '全角の123')
34
34
 
35
- >>> re.search(r'[0-9]', '半角の1')[0]
35
+ >>> re.search(r'[0-9]+', '半角の123')[0]
36
36
 
37
- '1'
37
+ '123'
38
38
 
39
39
  ```

3

数字の取り出し例追記

2020/01/23 20:16

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -21,3 +21,19 @@
21
21
  2行目があり、かつ、2行目に数字が含まれています
22
22
 
23
23
  ```
24
+
25
+
26
+
27
+ ```python
28
+
29
+ >>> re.search(r'\d+', '全角の1')[0]
30
+
31
+ '1'
32
+
33
+ >>> re.search(r'[0-9]', '全角の1')
34
+
35
+ >>> re.search(r'[0-9]', '半角の1')[0]
36
+
37
+ '1'
38
+
39
+ ```

2

split結果表示追加

2020/01/23 20:13

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -2,7 +2,11 @@
2
2
 
3
3
  >>> import re
4
4
 
5
- >>> text = "line1\nline2\nline3"
5
+ >>> text = 'line1\nline2\nline3\nline4\n'
6
+
7
+ >>> text.split('\n',2)
8
+
9
+ ['line1', 'line2', 'line3\nline4\n']
6
10
 
7
11
  >>> if '\n' in text and re.search(r'\d', text.split('\n', 2)[1]):
8
12
 

1

表示文字列変更

2020/01/23 20:08

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -6,14 +6,14 @@
6
6
 
7
7
  >>> if '\n' in text and re.search(r'\d', text.split('\n', 2)[1]):
8
8
 
9
- ... print('2行目があり、数字が含まれています')
9
+ ... print('2行目があり、かつ、2行目に数字が含まれています')
10
10
 
11
11
  ... else:
12
12
 
13
- ... print('2行目がない、数字が含まれていません')
13
+ ... print('2行目がない、または、2行目に数字が含まれていません')
14
14
 
15
15
  ...
16
16
 
17
- 2行目があり、数字が含まれています
17
+ 2行目があり、かつ、2行目に数字が含まれています
18
18
 
19
19
  ```