简体   繁体   English

Python re,如何捕获 12"" / 14""

[英]Python re, how to capture 12"" / 14""

I need to capture patterns like this one:我需要捕捉这样的模式:

12"" / 14""

in

"Factory SP1 150 12"" / 14"""

The numbers change (always 2 digits), the rest doesn't.数字会改变(总是 2 位数字),其余的不会。
Note that the double quotes at the ends of the string are part of the string and not enclosers.请注意,字符串末尾的双引号是字符串的一部分,而不是封闭符。

Also note that I'm working with pandas and using .str.extract(pattern) .另请注意,我正在使用熊猫并使用.str.extract(pattern)

My code:我的代码:

df = pd.read_csv(r'filename.csv', delimiter = ';', usecols = ["OLD_COLUMN", "OTHER_COLUMNS"], encoding='utf-8', error_bad_lines=False)

pattern = r'(\d{2}""\s*/\s*\d{2}"")'

df["NEW_COLUMN"] = df["OLD_COLUMN"].str.extract(pattern)

I changed groups, tried to escape every character.我换了组,试图逃避每个角色。 I can't find a way.我找不到办法。

You can use r'\d{2}""\s*/\s*\d{2}""' as regex:您可以使用r'\d{2}""\s*/\s*\d{2}""'作为正则表达式:

s = '"Factory SP1 150 12"" / 14"""'
re.findall(r'\d{2}""\s*/\s*\d{2}""', s)

output:输出:

['12"" / 14""']

Be careful with your strings: "Factory SP1 150 12"" / 14""" is equivalent to: "Factory SP1 150 12" + " / 14" + "" so 'Factory SP1 150 12 / 14'小心你的字符串: "Factory SP1 150 12"" / 14"""相当于: "Factory SP1 150 12" + " / 14" + ""所以'Factory SP1 150 12 / 14'

pattern = '([0-9]+""\s*/\s*[0-9]+"")'

Is a regex that will match that along with other expressions like 1351""/1"" .是一个正则表达式,它将与其他表达式(如1351""/1""匹配。 The issue is your use of the r or raw string.问题是您使用r或原始字符串。 It causes your \ in the pattern to be interpreted as literally \ .它会导致模式中的\被解释为字面意思\ So your original pattern would only match strings like 12\"\" / 14\"\"所以你的原始模式只会匹配像12\"\" / 14\"\"这样的字符串

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

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