简体   繁体   English

从字符串 Python 中删除撇号(如果它们之前和/或之后有空格)

[英]Remove apostrophes from string Python if they have space before and/or after them

I have a text:我有一段文字:

text = "the march' which 'cause those it's good ' way"

I need to remove all apostrophes in the text if they have space before and/or after them:如果它们之前和/或之后有空格,我需要删除文本中的所有撇号:

"the march which cause those it's good way"

I tried:我试过了:

re.sub("(?<=\b)'[a-z](?=\b)", "", text)

and

re.sub("\s'w+", " ", text)

But neither way seems to work for me但是这两种方法似乎都不适合我

You can use replace() method of string to achieve this.您可以使用字符串的 replace() 方法来实现这一点。 As below:如下:

text = "the march' which 'cause those it's good ' way"
new_text = text.replace("' "," ").replace(" ' "," ") 

You could get this done by contemplating the three different possibilities, and chaining them with |您可以通过考虑三种不同的可能性来完成此操作,并将它们与|链接起来。 taking care of the order:照顾订单:

re.sub(r"(\s\'\s)|(\s\')|(\'\s)", ' ', text)
# "the march which cause those it's good way"

See demo查看演示


  • (\s\'\s)|(\s\')|(\'\s)

    • 1st Alternative (\s\'\s)第一种选择(\s\'\s)

      • 1st Capturing Group (\s\'\s)第一个捕获组(\s\'\s)

      • \s matches any whitespace character (equal to [\r\n\t\f\v ] ) \s匹配任何空白字符(等于[\r\n\t\f\v ]

      • \' matches the character ' literally (case sensitive) \'匹配字符 ' 字面意思(区分大小写)
      • \s matches any whitespace character (equal to [\r\n\t\f\v ] ) \s匹配任何空白字符(等于[\r\n\t\f\v ]
    • 2nd Alternative (\s\')第二种选择(\s\')
      • 2nd Capturing Group (\s\')第二个捕获组(\s\')
      • \s matches any whitespace character (equal to [\r\n\t\f\v ] ) \s匹配任何空白字符(等于[\r\n\t\f\v ]
      • \' matches the character ' literally (case sensitive) \'匹配字符 ' 字面意思(区分大小写)
    • 3rd Alternative (\'\s)第三种选择(\'\s)
      • 3rd Capturing Group (\'\s)第三捕获组(\'\s)
      • \' matches the character ' literally (case sensitive) \'匹配字符 ' 字面意思(区分大小写)
      • \s matches any whitespace character (equal to [\r\n\t\f\v ] ) \s匹配任何空白字符(等于[\r\n\t\f\v ]

Maybe...也许...

(\s'\s?|'\s)

Given:鉴于:

"the march' which 'cause those it's good ' way"

Replace with: a space, ie, " "替换为:空格,即“”

Output: Output:

"the march which cause those it's good way"

Only 131 steps.只有 131 步。

Demo: https://regex101.com/r/x04Vg1/1演示: https://regex101.com/r/x04Vg1/1

Assuming you wish remove any extra spaces when a single quote surrounded by spaces is removed, you could use the following regular expression.假设您希望在删除由空格包围的单引号时删除任何额外的空格,您可以使用以下正则表达式。

(?<= ) *' +|'(?= )|(?<= )'

Regex demo正则表达式演示

import re
re.sub("(?<= ) *' +|'(?= )|(?<= )'", '', str)

Python demo Python 演示

Python's regex engine performs the following operations. Python 的正则表达式引擎执行以下操作。

(?<= )  # The following match must be preceded by a space  
 *      # match 0+ spaces
'       # match a single paren
 +      # match 1+ spaces
|       # or
'       # match a single paren
(?= )   # single paren must be followed by a space
|       # or
(?<= )  # The following match must be preceded by a space  
'       # match a single paren

(?<= ) is a postive lookbehind ; (?<= )积极的后视 (?= ) is a postive lookahead . (?= )是一个积极的前瞻

Note that this causes problems with "Gus' gal" and "It 'twas the night before the big bowling match", where the single quotes should not be removed.请注意,这会导致“Gus' gal”和“It'twas before the big bowling match”出现问题,其中不应删除单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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