Python Re,如何捕获12&quot' / 14“”
我需要捕获这样的模式:
12"" / 14""
在
"Factory SP1 150 12"" / 14"""
数字中更改(总是2位数字),其余的不会。
请注意,字符串末端的双引号是字符串的一部分,而不是封装器。
另请注意,我正在与Pandas合作,并使用.str.Extract(staters)
。
我的代码:
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 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.
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)
.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是一条正则态度,将与其他表达式匹配,例如
1351“/1”“”
。问题是您使用r
或原始字符串。它在模式中导致您的\
被解释为字面意义\
。因此,您的原始图案只能匹配12 \“ \” / 14 \“ \” < / code>之类的字符串
Is a regex that will match that along with other expressions like
1351""/1""
. The issue is your use of ther
or raw string. It causes your\
in the pattern to be interpreted as literally\
. So your original pattern would only match strings like12\"\" / 14\"\"
您可以使用
r'\ d {2}“” \ s*/\ s*\ d {2}“”'
as Regex:uption:输出:
请小心您的字符串:
sp1 150 12“” / 14“”“ < / code>等效于:
“工厂SP1 150 12” +“ / 14” +“” < / code> so
'工厂SP1 SP1 150 12/14 '
You can use
r'\d{2}""\s*/\s*\d{2}""'
as regex:output:
Be careful with your strings:
"Factory SP1 150 12"" / 14"""
is equivalent to:"Factory SP1 150 12" + " / 14" + ""
so'Factory SP1 150 12 / 14'