Home > Blockchain >  RegEx to exclude a space between country code and phone number itself?
RegEx to exclude a space between country code and phone number itself?

Time:10-07

My goal is to create two groups from a full phone number(which is given in a string):

First group: xyz (any three numbers with a plus sign)

Second group: following 7 or 8 digits

So, my regex is: ((\D?[0-9]{3})(\D?\s?[0-9]{7,8}))

The problem I encountered was that if I have, for example 358 55667788, whitespace after a country code is still included in the second group. Is there any way how to fix this?

String I have looks like this. " 347 56887364 37256887364 33359835647"

CodePudding user response:

You could use this regex pattern:

\ \d{3}[ ]?\d{7,8}\b

Sample script using re.findall:

inp = "Here are some nums:  347 56887364  37256887364  33359835647"
nums = re.findall(r'\ \d{3}[ ]?\d{7,8}\b', inp)
print(nums)  # [' 347 56887364', ' 37256887364', ' 33359835647']
  • Related