代码之家  ›  专栏  ›  技术社区  ›  Arkistarvh Kltzuonstev

在python中使用regex拆分字符串

  •  0
  • Arkistarvh Kltzuonstev  · 技术社区  · 8 年前

    a = 'avg yearly income 25,07,708.33 '
    b = 'current balance 1,25,000.00 in cash\n'
    c = 'target savings 50,00,000.00 within next five years 1,000,000.00 '
    

    我试图将它们分成文本字符串和数字字符串,输出示例如下:

    aa = [('avg yearly income', '25,07,708.33')]
    bb = [('current balance', '1,25,000.00', 'in cash')]
    cc = [('target savings', '50,00,000.00', 'within next five years', '1,000,000.00')]
    

    import re
    b = b.replace("\n","")
    aa = re.findall(r'(.*)\s+(\d+(?:,\d+)*(?:\.\d){1,2})', a)
    bb = re.findall(r'(.*)\s+(\d+(?:,\d+)*(?:\.\d){1,2})(.*)\s+', b)
    cc = re.findall(r'(.*)\s+(\d+(?:,\d+)*(?:\.\d){1,2})(.*)\s+(\d+(?:,\d+)*(?:\.\d{1,2})?)', c)
    

    我得到以下输出:

    aa = [('avg yearly income', '25,07,708.3')]
    bb = [('current balance', '1,25,000.0', '0 in')]
    cc = [('target savings', '50,00,000.0', '0 within next five years', '1,000,000.00')]
    

    正则表达式的模式有什么问题?

    4 回复  |  直到 8 年前
        1
  •  2
  •   Ajax1234    8 年前

    而不是 re.findall ,您可以使用 re.split

    import re
    d = ['avg yearly income 25,07,708.33 ', 'current balance 1,25,000.00 in cash\n', 'target savings 50,00,000.00 within next five years 1,000,000.00 ']
    final_results = [re.split('(?<=[a-zA-Z])\s(?=\d)|(?<=\d)\s(?=[a-zA-Z])', i) for i in d]
    new_results = [[i.rstrip() for i in b] for b in final_results]
    

    输出:

    [['avg yearly income', '25,07,708.33'], ['current balance', '1,25,000.00', 'in cash'], ['target savings', '50,00,000.00', 'within next five years', '1,000,000.00']]
    
        2
  •  1
  •   Sunitha    8 年前

    你可以用 re.split 与ptrn r'(?<=\d)\s+(?=\w)|(?<=\w)\s+(?=\d)'

    >>> ptrn = r'(?<=\d)\s+(?=\w)|(?<=\w)\s+(?=\d)'
    >>> re.split(ptrn, a)
    ['avg yearly income', '25,07,708.33 ']
    >>> re.split(ptrn, b)
    ['current balance', '1,25,000.00', 'in cash\n']
    >>> re.split(ptrn, c)
    ['target savings', '50,00,000.00', 'within next five years', '1,000,000.00 ']
    
        3
  •  0
  •   nosklo    8 年前

    re.split() ;此示例使用原始regexp,工作正常:

    >>> r = re.compile(r'(\d+(?:,\d+)*(?:\.\d{1,2}))')
    >>> r.split('avg yearly income 25,07,708.33 ')
    ['avg yearly income ', '25,07,708.33', ' ']
    >>> r.split('current balance 1,25,000.00 in cash\n')
    ['current balance ', '1,25,000.00', ' in cash\n']
    >>> r.split('target savings 50,00,000.00 within next five years 1,000,000.00 ')
    ['target savings ', '50,00,000.00', ' within next five years ', '1,000,000.00', ' ']
    
        4
  •  0
  •   VNS    8 年前

    你可以使用以上答案中的split。

    import re
    a = 'avg yearly income 25,07,708.33 '
    b = 'current balance 1,25,000.00 in cash\n'
    c = 'target savings 50,00,000.00 within next five years 1,000,000.00 '
    
    aa = re.split(r'(\d+(?:,\d+)*(?:\.\d{1,2}))', a)
    bb = re.split(r'(\d+(?:,\d+)*(?:\.\d{1,2}))', b)
    cc = re.split(r'(\d+(?:,\d+)*(?:\.\d{1,2}))', c)
    
    print(aa)
    print(bb)
    print(cc)
    

    你可以像

    ['avg yearly income ', '25,07,708.33', ' ']
    ['current balance ', '1,25,000.00', ' in cash\n']
    ['target savings ', '50,00,000.00', ' within next five years ', '1,000,000.00', ' ']