0

I want to implement a function to remove the punctuations, and I got the answer from a tutorial.

def filter_punctuation(words): 
    new_words = [];
    illegal_char = string.punctuation  
    pattern=re.compile('[%s]' % re.escape(illegal_char))
    for word in words:
        new_word = pattern.sub(u'', word) 
        if not new_word == u'':
            new_words.append(new_word) 
    return new_words

I am confused about the pattern=re.compile('[%s]' % re.escape(illegal_char)) part, I have no idea what the [%s] means. Can anyone help me?

Arkistarvh Kltzuonstev
  • 6,324
  • 6
  • 21
  • 43
Lilian
  • 11
  • 1
  • 1
    A regex like `[a]` is a character class. See [this answer](https://stackoverflow.com/a/1553171/3832970). – Wiktor Stribiżew Mar 14 '19 at 13:18
  • 1
    the `%s` is a string formatting thing, nothin directly to do with regex, take a look [here](https://docs.python.org/2/library/stdtypes.html#string-formatting) for (old style)formatting documentation – Nullman Mar 14 '19 at 13:20

0 Answers0