looking for help applying a regex function that finds a string that starts with 5 and is 7 digits long.
this is what i have so far based on my searches but doesn't work:
import re
string = "234324, 5604020, 45309, 45, 55, 5102903"
re.findall(r'^5[0-9]\d{5}', string)
not sure what i'm missing.
thanks
You are using a ^
, which asserts position at the start of the string. Use a word boundary instead. Also, you don't need both the [0-9]
and the \d
.
Use \b5[0-9]{6}\b
(or \b5\d{6}\b
) instead:
>>> re.findall(r'\b5\d{6}\b', s)
['5604020', '5102903']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With