Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in python, find a range of lengths

Tags:

python

regex

I'm doing a regex on a string to find a certain pattern:

fileName = '123456_BI12554_AA_0021.jpg'
id = re.findall(r'(_BI\d{5}_)', fileName)

Which finds _BI12554_ but sometimes there are 6 digits and not just 5.

Is there a way to find all the digits between _BI and _ regardless of how many there are?

like image 261
Richard Avatar asked Dec 06 '25 14:12

Richard


1 Answers

Yes of course: you can use + to mean "1 or more" digits.

re.findall(r'(_BI\d+_)', fileName)
like image 135
Daniel Roseman Avatar answered Dec 08 '25 04:12

Daniel Roseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!