【发布时间】:2019-10-14 17:45:12
【问题描述】:
我有字符串:
s = 'travel to africa x 2\ asia x 2\ europe x 2\ Airport pick up included. Furnitures 3 seater couch x 1 4 seater+ couch x 1 < 60 inches TV x 1 60 inches+ TV x 1 Washer - front loader x 1 Box / bag / misc x 1 The maximum clearance is 1.5m.'
我想将其拆分为x 并在其后提取数字。
所以预期的输出是:
out = [('travel to africa', '2'),
('\ asia', '2'),
( '\ europe', '2'),
('\ Airport pick up included. Furnitures 3 seater couch', '1'),
('4 seater+ couch', '1'),
('< 60 inches TV', '1'),
('60 inches+ TV', '1'),
('Washer - front loader', '1'),
('Box / bag / misc', '1')]
我尝试了这个正则表达式,但失败了,因为像 -+< 这样的特殊字符被省略了(也应该有另一个特殊字符):
r'([A-Za-z 0-9]+)\s+x\s+(\d+)'
提取这些值的正确正则表达式是什么?还是没有正则表达式的可能解决方案?
【问题讨论】:
-
你不是拆分,而是提取,试试
re.findall(r'(.*?)\s+x\s*(\d+)', s),见ideone.com/fclZme -
是的,但这里是系列,首先是所有数据帧
标签: python regex list split integer