【问题标题】:Regular Expressions How to append matches from raw string text into a list正则表达式如何将原始字符串文本中的匹配项附加到列表中
【发布时间】:2020-02-16 20:44:10
【问题描述】:

您好,我有一些乱七八糟的文本,我无法以任何好的方式处理,我想匹配原始字符串中的所有邮政编码 5 位数字,然后将它们附加到列表中。我的字符串看起来像这样:

string = '''
         January 2020
          Zip Code                                                                                            
           Current Month
        Sales Breakdown
      (by type)
              Last Month                Last Year     Year-to-Date
         95608
         Carmichael
         95610
        Citrus Heights
        95621
        Citrus Heights
         95624
         Elk Grove
         95626
         Elverta
         95628
         Fair Oaks
          95630
           Folsom
          95632
         Galt
        95638
        Herald
        95641
         Isleton
         95655
        Mather
       95660
        North Highlands
        95662
        Orangevale
            Total Sales                                                                                         
         43               REO Sales 0                                                      45                
        40                             43
           Median Sales Price                                                             $417,000             
                  $0                                      $410,000                 $400,000     
        $417,000
        '''

【问题讨论】:

  • zips = re.findall(r'\b\d{5}\b', string)
  • 到目前为止你有什么尝试?
  • Ups,对不起@Nick,我之前没有看到你的评论来发布我的答案。
  • @Ivanhercaz 没问题;我本可以回答但没有回答,因为我希望这个问题会作为重复而被关闭。
  • 是的,我也完全同意:(

标签: python regex


【解决方案1】:

可以使用re.findall 和正则表达式\b\d{5}\b 甚至只是\d{5} 来完成。我们来看一个例子:

import re

string = '''
         January 2020
          Zip Code                                                                                            
           Current Month
        Sales Breakdown
      (by type)
              Last Month                Last Year     Year-to-Date
         95608
         Carmichael
         95610
        Citrus Heights
        95621
        Citrus Heights
         95624
         Elk Grove
         95626
         Elverta
         95628
         Fair Oaks
          95630
           Folsom
          95632
         Galt
        95638
        Herald
        95641
         Isleton
         95655
        Mather
       95660
        North Highlands
        95662
        Orangevale
            Total Sales                                                                                         
         43               REO Sales 0                                                      45                
        40                             43
           Median Sales Price                                                             $417,000             
                  $0                                      $410,000                 $400,000     
        $417,000
        '''

regex = r'\b\d{5}\b'

zip_codes = re.findall(regex, string)

然后您可以从zip_codes 获取每个代码。我建议您阅读re documentationRegular Expression HOWTO。有一些有趣的工具可以编写和测试正则表达式,例如Regex101

我还建议您,下次您询问时,请自己进行一些调查,然后尝试做您想做的事情,然后,如果您有问题,请询问此特定问题。帮助页面How I ask a good question?How to create a Minimum, Reproducible example 可能会帮助您写出一个好问题。

【讨论】:

  • 如果否决票是由于我的回答中的一些错误,我将非常感激知道并学习!提前致谢。
猜你喜欢
  • 1970-01-01
  • 2012-05-23
  • 2017-08-01
  • 2021-06-02
  • 2013-08-30
  • 1970-01-01
  • 2016-01-29
相关资源
最近更新 更多