s='123AA456B789AAA'
# 想分割成:123AA 456B 789AAA
1
xpresslink 2019-11-20 20:58:31 +08:00 1
非要用 split 么?
>>> s='123AA456B789AAA' >>> import re >>> re.findall('\d+[AB]+',s) ['123AA', '456B', '789AAA'] |
2
sunhk25 OP @xpresslink 怎么都可以,不过分隔符 AA B AAA 都是不固定的
|
3
ddzzhen 2019-11-20 21:37:17 +08:00 via Android 1
分割的依据都没说,你指望得到解决方案?
|
5
ksedz 2019-11-20 22:46:50 +08:00
split 的话大概是这样,如果要连起来,像#1 说的 findall 比较好
```python In [1]: import re In [2]: s = '123AA456B789AAA' In [3]: re.split(r"[AB]", s) Out[3]: ['123', '', '456', '789', '', '', ''] In [4]: re.split(r"([AB])", s) Out[4]: ['123', 'A', '', 'A', '456', 'B', '789', 'A', '', 'A', '', 'A', ''] In [5]: re.split(r"([AB]+)", s) Out[5]: ['123', 'AA', '456', 'B', '789', 'AAA', ''] ``` |
6
cherbim 2019-11-20 23:40:55 +08:00
要是分隔符只为字母,建议正则
|
7
necomancer 2019-11-21 00:13:42 +08:00
一楼答案没毛病啊
In [1]: re.findall(r"\d+[A-Z]+", s) Out[1]: ['123AA', '456B', '789AAA'] |