I am aiming for regex code to grab phone number and remove unneeded characters.
import re
strs = 'dsds +48 124 cat cat cat245 81243!!'
match = re.search(r'.[ 0-9\+\-\.\_]+', strs)
if match:
print 'found', match.group() ## 'found word:cat'
else:
print 'did not find'
It returns only:
+48 124
How I can return the entire number?
Remove Special Characters From the String in Python Using the str. isalnum() Method. The str. isalnum() method returns True if the characters are alphanumeric characters, meaning no special characters in the string.
You want to use sub()
, not search()
:
>>> strs = 'dsds +48 124 cat cat cat245 81243!!'
>>> re.sub(r"[^0-9+._ -]+", "", strs)
' +48 124 245 81243'
[^0-9+._ -]
is a negated character class. The ^
is significant here - this expression means: "Match a characters that is neither a digit, nor a plus, a dot, an underscore, a space or a dash".
The +
tells the regex engine to match one or more instances of the preceding token.
The problem with re.sub()
is that you get extra spaces in your final phone number string. The non-regular expression way, which returns the correct phone number (without any spaces):
>>> strs = 'dsds +48 124 cat cat cat245 81243!!'
>>> ''.join(x for x in strs if x.isdigit() or x == '+')
'+4812424581243'
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