Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex starting with number 5

Tags:

python

regex

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

like image 387
orangecodelife Avatar asked Dec 23 '22 04:12

orangecodelife


1 Answers

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']
like image 130
user3483203 Avatar answered Jan 01 '23 18:01

user3483203