回答編集履歴

2

追伸

2022/12/05 09:36

投稿

insecticide
insecticide

スコア315

test CHANGED
@@ -1,15 +1,23 @@
1
1
  melian さまより
2
2
  ```python
3
- import re
3
+ import re # re: Python の正規表現(REgular expression)モジュールです。
4
4
  text = "aaabbb[xyze]cccddd(1234)[kjuh]XYZ"
5
- print([*filter(None, re.split(r'\[.*?\]|\(.*?\)', text))])
5
+ print([*filter(None, re.split(r'\[.*?\]|\(.*?\)', text))]) # 区切り記号のパターンを定義:『.*?』は最短マッチング;『.*』は最長マッチング
6
6
  ```
7
7
 
8
8
  <結果>
9
9
  ['aaabbb', 'cccddd', 'XYZ']
10
10
 
11
+ ======ついでに========
12
+ 最長マッチングで区切りパターンを定義した場合:
13
+ ```python
14
+ print([*filter(None, re.split(r'\[.*\]|\(.*?\)', text))])
15
+ ```
16
+
17
+
18
+ <結果>
19
+ ['aaabbb', 'XYZ']
11
20
 
12
21
 
13
22
 
14
23
 
15
-

1

追伸

2022/12/05 09:28

投稿

insecticide
insecticide

スコア315

test CHANGED
@@ -5,6 +5,11 @@
5
5
  print([*filter(None, re.split(r'\[.*?\]|\(.*?\)', text))])
6
6
  ```
7
7
 
8
+ <結果>
9
+ ['aaabbb', 'cccddd', 'XYZ']
8
10
 
9
11
 
10
12
 
13
+
14
+
15
+