简体   繁体   中英

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

The ID can have multiple digits.

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' .

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('_')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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