Remove unwanted characters from phone number string
python, regex
Solution
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.
Problem
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?