简体   繁体   中英

python 3 find a string that is not followed by a substring

I have a string that looks like this:

line = "aaa farmer's blooper's mouse'd would've bbb"

From my string, line, if a word ends with apostrophe or apostrophe + s ('s), then I want to drop apostrophe and s. But I want to keep the word unchanged otherwise.

So, I want my string to be,

line = "aaa farmer blooper mouse'd would've bbb"

How can this be done using regex expressions?

using regex lookahead to assert what follow by apostrophe, or apostrophe + s ('s) can be whitespace character only or end of string, or in other words, end of words or end of string

import re
line = "aaa farmer's blooper's mouse'd would've bbb"
line_new = re.sub(r"'s?(?=(\s|$))", '', line)
# "aaa farmer blooper mouse'd would've bbb"

regex explanation

  • 's? # match apostrophe follow by zero or one s
  • (?=(\\s|$)) # assert what follow by can be whitespace character only or end of string

another alternative of the regex would be using negative lookahead to assert what follow by is not any non-whitespace character re.sub(r"'s?(?!\\S)", '', line)

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