简体   繁体   中英

Remove string contents after apostrophe

I am trying to remove string content after apostrophe punctucation mark ' from a sentence.

For example, given a sentence This is the world's first engine. You'll learn more about this later. This is the world's first engine. You'll learn more about this later.

The expected output is This is the world first engine. You learn more about this later. This is the world first engine. You learn more about this later.

I am able to remove the apostrophe ' using re.sub() . However, I am unable to remove the contents after the apostrophe

re.sub(r'[^\\w]', ' ', value)

I have used python replace() as well. However, that would not be a generic solution

value.replace("'s", "")

Any help is appreciated. Thank You!

To remove apostrophes and trailing letters within your str you could use the below code.

import re

s = "This is the world's first engine. You'll learn more about this later."
s = re.sub(r'\'\w+', '', s)
print(s)

Output : This is the world first engine. You learn more about this later. This is the world first engine. You learn more about this later.

The pattern \\'\\w+ matches apostrophes followed by one or more word characters, re.sub() is used to replace any matches of this pattern with an empty string ( '' ).

You can use re.sub() as follows

import re
def clean(value):
    return re.sub(r'\'\w{,2}', '', value)

print(clean("This is the world's first engine. You'll learn more about this later"))

Try this (i guess there are simpler solutions with regex though):

s="This is the world's first engine. You'll learn more about this later."

s=' '.join(list(map(lambda x: x[:x.find("'")] if "'" in x else x, s.split(' '))))

>>> print(s)
'This is the world first engine. You learn more about this later.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM