简体   繁体   English

在 python 中使用正则表达式提取字符串的一部分

[英]Extracting part of a string using regex in python

I realize this question has been asked a million times, but I'm not very good with regex yet.我意识到这个问题已经被问了一百万次,但我对正则表达式还不是很好。 I'm trying to extract a company name from a file name.我正在尝试从文件名中提取公司名称。 The file name structure is as such:文件名结构如下:

ID_Name_analytics.png

For example: 3_Football Company_analytics.png例如: 3_Football Company_analytics.png

The ID can have multiple digits. ID 可以有多个数字。

I've tried我试过了

organisation_name = re.search(r"^\d+_(\w+)_PYP_analytics\.png$", visual_file).group(0)

but it gives me AttributeError: 'NoneType' object has no attribute 'groups' .但它给了我AttributeError: 'NoneType' object has no attribute 'groups'

What am I doing wrong?我究竟做错了什么?

You do not need regex for this.您不需要正则表达式。

my_string = "3_Football Company_analytics.png"

organization_id, organization_name, _ = my_string.split('_')

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

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