1

I want to remove special characters from start or end of string,

@Can't& 

Using regular expression and I've tired,

`[^\w\s]` 

But this regular expression removes ' which is inside the word and return below word,

Cant 

Can't seem to wrap my head around this any ideas would be highly appreciated.

0

2 Answers 2

3

Can be simplified like this:

 res = re.sub(r'^\W+|\W+$', '', txt) 
Sign up to request clarification or add additional context in comments.

Comments

2

Use the following approach (using regex alternation ..|..):

import re s = "@Can't&" res = re.sub(r'^[^\w\s]+|[^\w\s]+$', '', s) print(res) # Can't 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.