1

Given an array of strings which contains alphanumeric characters but also punctuations that have to be deleted. For instance the string x="0-001" is converted into x="0001".

For this purpose I have:

punctuations = list(string.punctuation)

Which contain all the characters that have to be removed from the strings. I'm trying to solve this using regular expressions in python, any suggestion on how to proceed using regular expressions?

import string
punctuations = list(string.punctuation)
test = "0000.1111"
for i, char in enumerate(test): 
    if char in punctuations:
       test = test[:i] + test[i+ 1:]
Melvin
  • 45
  • 8

1 Answers1

3

If all you want to do is remove non-alphanumeric characters from a string, you can do it simply with re.sub:

>>> re.sub('\W', '', '0-001')
'0001'

Note, the \W will match any character which is not a Unicode word character. This is the opposite of \w. For ASCII strings it's equivalent to [^a-zA-Z0-9_].

randomir
  • 17,989
  • 1
  • 40
  • 55