1

I have strings consisting of letters and numbers, like:

Ex1: "Phone 18020210 914 171 717 mailbox 43, 1003 Florvaag"

Ex2: "Phone 18020210 N 0914 171 717 mailbox 43, 1003 Florvaag"

Ex3: "Phone 18020210 N0914 171 717 mailbox 43, 1003 Florvaag"

I would like to find the numbers that occurs like "914 171 717" and "0210 914 171", eg. the nine digit number can either be preceded by a zero, or a space.

I tried this:

re.findall('[\s|0]\d{3}\s\d{3}\s\d{3}\s',Ex1)

But this expression only returns ['0210 914 171 '], while I would like it to return both numbers : ['0210 914 171 ','914 171 717']

It is probably a quick fix, but I couldn't figure out how to do it.

Pyders
  • 33
  • 3

1 Answers1

1

You can use

re.findall(r'\b0?\d{3}\s\d{3}\s\d{3}\b', text)

See the regex demo.

Details:

  • \b - a word boundary
  • 0? - an optional 0
  • \d{3} - three digits
  • \s - a whitespace
  • \d{3} - three digits
  • \s\d{3} - whitespace, three digits
  • \b - a word boundary.

See the Python demo:

import re
s = 'Phone 18020210 914 171 717 mailbox 43, 1003 Florvaag\nEx2: "Phone 18020210 N 0914 171 717 mailbox 43, 1003 Florvaag\nPhone 18020210 N0914 171 717 mailbox 43, 1003 Florvaag'
print( re.findall(r'\b0?\d{3}\s\d{3}\s\d{3}\b', s) )
# -> ['914 171 717', '0914 171 717']
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563