[英]How to find a specific piece of text in a string with regex and python
I have a text in every cell of a column, where i want to get some information from.我在一列的每个单元格中都有一个文本,我想从中获取一些信息。 In every cell i have detailed information about cars and i need to get the text from it.
在每个单元格中,我都有关于汽车的详细信息,我需要从中获取文本。 In my case these are the fuel and the CO2 information.
就我而言,这些是燃料和二氧化碳信息。
The string, that i get, looks like this:我得到的字符串如下所示:
cell 1 = 17.160 km, 80 kW (109 PS)Limousine, Autogas (LPG), Automatik, HU Neu, 2/3 Türenca.单元 1 = 17.160 km,80 kW (109 PS)Limousine,Autogas (LPG),Automatik,HU Neu,2/3 Türenca。 5,0 l/100km (komb.), ca.
5,0 l/100km (komb.), ca. 116 g CO₂/km (komb.)
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. 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.
5,9 l/100km (komb.), ca. 134 g CO₂/km (komb.) ... and so on
134 g CO₂/km (komb.) ...等等
so i need the information from cell 1: 5,0 l/100 km and 116 g CO2/km所以我需要来自单元格 1 的信息:5,0 l/100 km 和 116 g CO2/km
and from cell 2: 5,9 l/100km and 134 g CO2/km来自电池 2:5,9 l/100km 和 134 g CO2/km
I tried the following code examples, but nothing worked:我尝试了以下代码示例,但没有任何效果:
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]?')
and after each "pattern_z" variable i tried在我尝试过的每个“pattern_z”变量之后
co = pattern_z.search(i)
cox = co.group()
but nothing worked.但没有任何效果。
I would appreciate every help.我将不胜感激每一个帮助。
Use用
(\d+(?:,\d+)?\s*l/\d+km).*?(\d+\s?g\s*CO[₂2]/km)
See regex proof .请参阅正则表达式证明。
EXPLANATION解释
--------------------------------------------------------------------------------
( 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
Python code : 蟒蛇代码:
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))
Results : [('5,0\ l/100km', '116\ g CO₂/km'), ('5,9\ l/100km', '134\ g CO₂/km')]
结果:
[('5,0\ l/100km', '116\ g CO₂/km'), ('5,9\ l/100km', '134\ g CO₂/km')]
You might use你可能会用
\b\d+(?:,\d+)?(?:\s*l/\d+|\s*g\s+CO₂/)km\b
\\b
A word boundary \\b
一个词边界\\d+(?:,\\d+)?
Match 1+ digits and an optional decimal part(?:
Non catpure group (?:
非捕获组
\\s*l/\\d+
match l/
and 1+ digits \\s*l/\\d+
匹配l/
和 1+ 数字|
Or\\s*g\\s+CO₂/
match g
, whitespace chars and CO₂/ \\s*g\\s+CO₂/
匹配g
、空白字符和 CO₂/)
Close non capture group )
关闭非捕获组km\\b
Match km
and a word boundary to prevent a partial match 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))
Output输出
['5,0 l/100km', '116 g CO₂/km']
['5,9 l/100km', '134 g CO₂/km']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.