5👍
✅
Have you considered using the built in slugify
filter?
The problem with your second expression is you are using a positive lookbehind (?<=
).
From regular-expressions.info:
“Zero-width positive lookbehind. Matches at a position if the pattern inside the lookahead can be
matched ending at that position (i.e. to the left of that position).
The following is probably what you were trying to do:
re.sub('[,.?!\t\n ]+', '-', s)
This is replacing any sequence of characters ,.?!\t\n
with a single dash.
Source:stackexchange.com