【问题标题】:Extract text and integer after specific letter提取特定字母后的文本和整数
【发布时间】: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')]

我尝试了这个正则表达式,但失败了,因为像 -+&lt; 这样的特殊字符被省略了(也应该有另一个特殊字符):

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


【解决方案1】:

你可以使用

re.findall(r'(.*?)\s+x\s*(\d+)', s)

请参阅Python demoregex demo

(.*?)\s+x\s*(\d+) 模式匹配

  • (.*?) - 第 1 组:除换行符以外的任何 0+ 个字符
  • \s+ - 1+ 个空格
  • x - x 字符
  • \s* - 0+ 个空格
  • (\d+) - 第 2 组:一位或多位数字。

如果您想在匹配开始时去掉空格,请使用re.findall(r'(\S.*?)\s+x\s*(\d+)', s)(请参阅regex demo)或在获得所有匹配后使用理解,[x.strip() for x in re.findall(r'(.*?)\s+x\s*(\d+)', s)]

【讨论】:

  • \s+x 之前,+1
【解决方案2】:

这是一种方法。我通过尝试匹配每个组来简化问题,然后手动拆分。

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.'
import re
res = []
for match in re.finditer(".*?x\s*\d+", s):
    l, _, r = map(str.strip, match.group().rpartition('x'))
    res.append((l, r))

输出:

[('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')]

【讨论】:

    【解决方案3】:

    我对问题的看法:

    import re
    import pprint
    
    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.'
    
    out = []
    
    for g in re.findall(r'(((^|\\?).*?)\s*x\s*(\d+)(.*?))', s):
        out += [[g[1], g[3]]]
    
    pprint.pprint(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']]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-30
      • 2021-12-10
      • 2021-12-19
      • 2021-12-19
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      相关资源
      最近更新 更多