1

I have the following test string:

test_str = `It isn't directed at all,' said the White Rabbit;

My current regular expression uses re.sub to filter out the punctuation so that I can do my own operations.

My current regex is re.sub(r"[^A-Za-z0-9'\s]", '', test_str)

The output from above is:

['It', "isn't", 'directed', 'at', "all'", 'said', 'the', 'White', 'Rabbit']

The error can be seen at all' when it is suppose to be storing all only.

How do you store words with 's and also ignore ' that comes after a punctuation? In this case, all,'.

3 Answers 3

1

Try the following:

import re
test_str = "`It isn't directed at all,' said the White Rabbit;"
a = re.sub(r"[^A-Za-z0-9'\s]", '', test_str)
a = re.sub(r"'[ ]", ' ', a)
print(a)
Sign up to request clarification or add additional context in comments.

Comments

1

Try using this regular expression:

print(re.sub('["!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~''](?!\w+)', '', test_str))

Output:

It isn't directed at all said the White Rabbit

1 Comment

I tried working on it but its a little bit complex from what i wanna achieve. Thank you so help for ur help !
0

Here are other solutions

re.sub("\'[^\w]",' ', test_str)
re.sub("\'[\s]",' ', test_str)
re.sub("\'(?!\w)",'', test_str)
re.sub("\'(?=\s)",'', test_str)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.