teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

7

プログラム変更

2020/01/24 04:12

投稿

shiracamus
shiracamus

スコア5406

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

6

空白挿入

2020/01/24 04:12

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  ```python
2
2
  >>> import re
3
3
  >>> text = 'line1\nline2\nline3\nline4\n'
4
- >>> text.split('\n',2)
4
+ >>> text.split('\n', 2)
5
5
  ['line1', 'line2', 'line3\nline4\n']
6
6
  >>> if '\n' in text and re.search(r'\d', text.split('\n', 2)[1]):
7
7
  ... print('2行目があり、かつ、2行目に数字が含まれています')

5

例追記

2020/01/23 21:06

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -14,7 +14,9 @@
14
14
  ```python
15
15
  >>> re.search(r'\d+', '全角の123')[0]
16
16
  '123'
17
- >>> re.search(r'[0-9]+', '全角の123')
18
- >>> re.search(r'[0-9]+', '半角の123')[0]
17
+ >>> re.search(r'\d+', '半角の123')[0]
19
18
  '123'
19
+ >>> re.search('[0-9]+', '全角の123')
20
+ >>> re.search('[0-9]+', '半角の123')[0]
21
+ '123'
20
22
  ```

4

複数数字に変更

2020/01/23 20:18

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -12,9 +12,9 @@
12
12
  ```
13
13
 
14
14
  ```python
15
- >>> re.search(r'\d+', '全角の1')[0]
15
+ >>> re.search(r'\d+', '全角の123')[0]
16
- '1'
16
+ '123'
17
- >>> re.search(r'[0-9]', '全角の1')
17
+ >>> re.search(r'[0-9]+', '全角の123')
18
- >>> re.search(r'[0-9]', '半角の1')[0]
18
+ >>> re.search(r'[0-9]+', '半角の123')[0]
19
- '1'
19
+ '123'
20
20
  ```

3

数字の取り出し例追記

2020/01/23 20:16

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -9,4 +9,12 @@
9
9
  ... print('2行目がない、または、2行目に数字が含まれていません')
10
10
  ...
11
11
  2行目があり、かつ、2行目に数字が含まれています
12
+ ```
13
+
14
+ ```python
15
+ >>> re.search(r'\d+', '全角の1')[0]
16
+ '1'
17
+ >>> re.search(r'[0-9]', '全角の1')
18
+ >>> re.search(r'[0-9]', '半角の1')[0]
19
+ '1'
12
20
  ```

2

split結果表示追加

2020/01/23 20:13

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -1,6 +1,8 @@
1
1
  ```python
2
2
  >>> import re
3
- >>> text = "line1\nline2\nline3"
3
+ >>> text = 'line1\nline2\nline3\nline4\n'
4
+ >>> text.split('\n',2)
5
+ ['line1', 'line2', 'line3\nline4\n']
4
6
  >>> if '\n' in text and re.search(r'\d', text.split('\n', 2)[1]):
5
7
  ... print('2行目があり、かつ、2行目に数字が含まれています')
6
8
  ... else:

1

表示文字列変更

2020/01/23 20:08

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -2,9 +2,9 @@
2
2
  >>> import re
3
3
  >>> text = "line1\nline2\nline3"
4
4
  >>> if '\n' in text and re.search(r'\d', text.split('\n', 2)[1]):
5
- ... print('2行目があり、数字が含まれています')
5
+ ... print('2行目があり、かつ、2行目に数字が含まれています')
6
6
  ... else:
7
- ... print('2行目がない、数字が含まれていません')
7
+ ... print('2行目がない、または、2行目に数字が含まれていません')
8
8
  ...
9
- 2行目があり、数字が含まれています
9
+ 2行目があり、かつ、2行目に数字が含まれています
10
10
  ```