orangecodelife
orangecodelife

Reputation: 143

regex starting with number 5

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

Upvotes: 4

Views: 4149

Answers (3)

The Scientific Method
The Scientific Method

Reputation: 2436

Try to match: Boundary 5 followed by 6 digits and after that match non-digit character in a non-capturing group.

\b5 looks 5 at start of numbers

\d{6} matches 6 digits
(?:\D|$) non-capturing group: ignores non-digit or $

\b5\d{6}(?:\D|$)

demo

import re

string = "234324, 5604020, 45309, 45, 55, 5102903"
re.findall(r'\b5\d{6}(?:\D|$)', string)

Upvotes: 1

user3483203
user3483203

Reputation: 51165

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

Upvotes: 6

Ruzihm
Ruzihm

Reputation: 20249

The ^ at the start of the regular expression forbids any matches that aren't at the very beginning of the string. Replacing it with a negative lookbehind for \d to match non-digits or the beginning, and add a negative lookahead to forbid extra following digits:

import re

string = "234324, 5604020, 45309, 45, 55, 5102903"
re.findall(r'(?<!\d)5\d{6}(?!\d)', string)

Upvotes: 3

Related Questions