python - how to ignore special characters when using word boundry -
s = '!sopa !sop !sopaa !sopii'
how ignore !
when using word boundary
re.sub(r'\b\!sop\b', 'sopa', s)
output : '!sopa !sop !sopaa !sopii'
seems want this.
>>> s = '!sopa !sop !sopaa !sopii' >>> re.sub(r'\b!sop\b', 'sopa', s) '!sopa sopa !sopaa !sopii'
your regex fail because there isn't \b
exits before !
symbol. is, above you're trying match !
symbol if it's preceded non-word character. \b
matches between word char , non-word character, vice versa. \b
matches between 2 word , 2 non-word chars. here \b
actaully exists between space , !
, since both non-word characters.
Comments
Post a Comment