繁体   English   中英

如何使用正则表达式和python在字符串中查找特定文本

[英]How to find a specific piece of text in a string with regex and python

我在一列的每个单元格中都有一个文本,我想从中获取一些信息。 在每个单元格中,我都有关于汽车的详细信息,我需要从中获取文本。 就我而言,这些是燃料和二氧化碳信息。

我得到的字符串如下所示:

单元 1 = 17.160 km,80 kW (109 PS)Limousine,Autogas (LPG),Automatik,HU Neu,2/3 Türenca。 5,0 l/100km (komb.), ca. 116 g CO₂/km (komb.)

cell 2 = EZ 10/2018, 12.900 km, 80 kW (109 PS)Limousine, Unfallfrei, Hybrid (Benzin/Elektro), Halbautomatik, HU Neu, ca. 5,9 l/100km (komb.), ca. 134 g CO₂/km (komb.) ...等等

所以我需要来自单元格 1 的信息:5,0 l/100 km 和 116 g CO2/km

来自电池 2:5,9 l/100km 和 134 g CO2/km

我尝试了以下代码示例,但没有任何效果:

    pattern_z = re.compile("[a-z]+.?\s?[0-9]+\s?[a-z]?\s[A-Z]+")
    pattern_z = re.compile("^[ac]+\s?[CO]$")
    pattern_z = re.compile(r'[0-9]+.[g]?')
    

在我尝试过的每个“pattern_z”变量之后

    co = pattern_z.search(i)
    cox = co.group()

但没有任何效果。

我将不胜感激每一个帮助。

(\d+(?:,\d+)?\s*l/\d+km).*?(\d+\s?g\s*CO[₂2]/km)

请参阅正则表达式证明

解释

--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
--------------------------------------------------------------------------------
      ,                        ','
--------------------------------------------------------------------------------
      \d+                      digits (0-9) (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    )?                       end of grouping
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    l/                       'l/'
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    km                       'km'
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    \s?                      whitespace (\n, \r, \t, \f, and " ")
                             (optional (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    g                        'g'
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    CO                       'CO'
--------------------------------------------------------------------------------
    [₂2]               any character of: '&', '#', '8', '3',
                             '2', '2', ';', '2'
--------------------------------------------------------------------------------
    /km                      '/km'
--------------------------------------------------------------------------------
  )                        end of \2

蟒蛇代码

import re

regex = r"(\d+(?:,\d+)?\s*l/\d+km).*?(\d+\s?g\s*CO[₂2]/km)"

test_str = "17.160 km, 80 kW (109 PS)Limousine, Autogas (LPG), Automatik, HU Neu, 2/3 Türenca. 5,0 l/100km (komb.), ca. 116 g CO₂/km (komb.)\n\nEZ 10/2018, 12.900 km, 80 kW (109 PS)Limousine, Unfallfrei, Hybrid (Benzin/Elektro), Halbautomatik, HU Neu, ca. 5,9 l/100km (komb.), ca. 134 g CO₂/km (komb.) ... and so on"

print (re.findall(regex, test_str))

结果: [('5,0\ l/100km', '116\ g CO₂/km'), ('5,9\ l/100km', '134\ g CO₂/km')]

你可能会用

\b\d+(?:,\d+)?(?:\s*l/\d+|\s*g\s+CO₂/)km\b
  • \\b一个词边界
  • \\d+(?:,\\d+)? 匹配 1+ 位数字和一个可选的小数部分
  • (?:非捕获组
    • \\s*l/\\d+匹配l/和 1+ 数字
    • | 或者
    • \\s*g\\s+CO₂/匹配g 、空白字符和 CO₂/
  • )关闭非捕获组
  • km\\b匹配km和单词边界以防止部分匹配

正则表达式演示

import re

strings = [
    '17.160 km, 80 kW (109 PS)Limousine, Autogas (LPG), Automatik, HU Neu, 2/3 Türenca. 5,0 l/100km (komb.), ca. 116 g CO₂/km (komb.)',
    'EZ 10/2018, 12.900 km, 80 kW (109 PS)Limousine, Unfallfrei, Hybrid (Benzin/Elektro), Halbautomatik, HU Neu, ca. 5,9 l/100km (komb.), ca. 134 g CO₂/km (komb.)'
    ]
pattern = r"\b\d+(?:,\d+)?(?:\s*l/\d+|\s*g\s+CO₂/)km\b"
for s in strings:
    print(re.findall(pattern, s))

输出

['5,0 l/100km', '116 g CO₂/km']
['5,9 l/100km', '134 g CO₂/km']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM