Regex not substituting string with spaces

Viewed 52

i have made this code to replace all the non alphabet with the space but i am not getting the spaces doing this is clearing all the spaces

edit = [["this, is sample 1."],["this!is @ sample two.*"]]
def clearpunch(sentence):
    clean = re.sub(r"'|\W|\d|_",r" ",sentence)
    return clean
edit = edit.map(clearpunch)
edit

getting text like this "[["thisissample"], ["thisissampletwo"]]"

and i want a text like "[["this is sample "], ["this is sample two "]]"

1 Answers

This gives you what you want, using the map function:

import re

def clearpunch(sentence):
    clean = re.sub(r"'|\W|\d|_",r" ",sentence)
    return clean

edit = [["this, is sample 1."],["this!is @ sample two.*"]]

answer = [list(map(clearpunch, item)) for item in edit]

print(answer)

Input:

[["this, is sample 1."],["this!is @ sample two.*"]]

Output:

[['this  is sample   '], ['this is   sample two  ']]

If you just want lowercase English letters you can use this regex pattern: r"[^a-z]"

Related