如何删除包含单词的撇号?

huangapple go评论130阅读模式
英文:

how to remove apostrophes that contain a word?

问题

我想删除包含"friend"但不包含"Alex's"中的撇号。如果我使用re.sub尝试如下:

list = re.sub('\'', '', list)

它会删除所有的撇号,这不是我想要的。我该如何实现这一目标,以便结果是:

list = ["I wish I was Alex's best friend"]
英文:

Let's say I have a list that goes something like:

list = [" I wish I was Alex's best 'friend' "]

I want to remove the apostrophe that contains friend but not the apostrophe that is used in Alex's, If I try this with re.sub like so:

list = re.sub('\'\')', '', list)

it removes all the apostrophes which I don't want, how can I achieve this so that the result is:

list = [" I wish I was Alex's best friend " ]

答案1

得分: 1

这种另一种做法是:

re.sub(r"(?<![a-zA-Z])'(.*?)'(?![a-zA-Z])", r'', list[0])

这段代码寻找一个撇号,然后是任何内容,然后是另一个撇号,只要第一个撇号前面或最后一个撇号后面没有字母。

英文:

Another way of doing it:

re.sub(r"(?<![a-zA-Z])'(.*?)'(?![a-zA-Z])", r'', list[0])

This looks for an apostrophe, followed by anything, followed by another apostrophe, as long as there isn't a letter just before the first apostrophe or just after the last one.

答案2

得分: 0

假设撇号只出现在单词周围而不是完整句子,您可以使用以下代码:

re.sub(r"'(\w+)'", "", your_string)
# 如果您遍历列表并对其进行迭代,your_string可以是列表中的字符串

这将捕捉被撇号包围的单词,并删除这些撇号。因此,如果只有一个撇号,例如 Alex'sit's,它不会将它们删除。

请注意,这种方法并不总是有效,例如,如果在 Alex's 周围有撇号,就无法知道要删除哪个撇号(虽然理论上可以,但这个正则表达式无法处理)。

更新

您可以使用贪婪(> 0)量词和备选语句来删除 Alex's 周围的撇号:

re.sub(r"'((?:\w|')+)'", "\1", your_string)
英文:

Assuming apostrophes are only around a single word at a time and not a full sentence, you can use this:

re.sub(r"'(\w+)'", "", your_string)
# your_string can be strings from your list if you iterate over it

This will catch words surrounded by apostrophes, and remove these apostrophes. Therefore, if there's only a single apostrophes as in Alex's or it's, it won't remove them.

Please be aware this will not always work, for example if there are apostrophes around Alex's, there's no way to know which apostrophe to remove (there is, but it wouldn't be handled by this regex)

Update

You can make use of a greedy (> 0) quantifier and an alternate statement in order to remove apostrophes around Alex's:

re.sub(r"'((?:\w|')+)'", "\1", your_string)

huangapple
  • 本文由 发表于 2023年7月13日 20:52:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定