首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在标点符号的特定列表后面和前面去掉空格

在标点符号的特定列表后面和前面去掉空格
EN

Stack Overflow用户
提问于 2018-09-07 18:13:30
回答 3查看 87关注 0票数 2

虽然我在StackOverflow中找到了一些引用,但我无法编写正确的正则表达式来实现我的目标。我想从python中的字符串中删除特定标点符号前后的空格。

我有一个功能如下。

代码语言:javascript
运行
复制
def modify_answers(answers):
    hyp = []
    for ans in answers:
        # remove whitespace before - / ? . ! ;
        newhyp = re.sub(r'\s([-/?.!,;](?:\s|$))', r'\1', ans)
        # remove whitespace after - / $ _
        newhyp = re.sub(r'', r'\1', newhyp)
        hyp.append(newhyp)
    return hyp

我想要取得的成就的一些例子:

  • 税号为1866-704-7388.
  • “不,emu在维多利亚不受保护。”--“不,emu在维多利亚不受保护。”
  • “查找就是丢失,就像构造是___。”
  • "$1,0等于$ 1,0“-> "$ 1,0等于$1,0”

任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-07 18:41:02

首先,定义一个执行替换的函数:

代码语言:javascript
运行
复制
import re

def replace(x):
    y, z = x.groups()
    if z in '-/?.!,;':
        y = y.lstrip()
    if z in '-/$_':
        y = y.rstrip()
    return y

该函数采用匹配模式,并相应地执行替换。

现在,定义你的模式。为了提高效率,您可以预编译.

代码语言:javascript
运行
复制
p = re.compile(r'(\s*([-/?.,!$_])\s*)')

使用前面定义的回调对每个字符串调用编译后的regex sub

代码语言:javascript
运行
复制
cases = [                               
    "Tax pin number is 1 - 866 - 704 - 7388 .",
    "No , emu is not protected in Victoria .",
    "Find is to lose as construct is to _ _ _ _ _ _ .",
    "$ 1,0 is equal to $ 1,0 ."]

repl = [p.sub(replace, c) for c in cases]

代码语言:javascript
运行
复制
print (repl)
['Tax pin number is 1-866-704-7388.', 'No, emu is not protected in Victoria.', 
 'Find is to lose as construct is to ______.', '$1,0 is equal to $1,0.']
票数 4
EN

Stack Overflow用户

发布于 2018-09-07 18:34:38

你可以这样做:

代码语言:javascript
运行
复制
import re

sentences = ["Tax pin number is 1 - 866 - 704 - 7388 .",
             "No , emu is not protected in Victoria .",
             "Find is to lose as construct is to _ _ _ _ _ _ .",
             "$ 1,0 is equal to $ 1,0 ."]


def modify_answers(answers):
    hyp = []
    for ans in answers:
        # remove whitespace before - / ? . ! ;
        new_hyp = re.sub(r'\s([/?.!;_-])(\s|$)', r'\1', ans)
        new_hyp = re.sub(r'\s(,)(\s|$)', r'\1 ', new_hyp)
        new_hyp = re.sub(r'(^|\s)(\$)(\s|$)', r' \2', new_hyp)
        hyp.append(new_hyp.strip())
    return hyp

for sentence in modify_answers(sentences):
    print(sentence)

输出

代码语言:javascript
运行
复制
Tax pin number is 1-866-704-7388.
No, emu is not protected in Victoria.
Find is to lose as construct is to______.
$1,0 is equal to $1,0.

Notes

  • 第一个正则表达式仅用符号替换任何被空格包围的/?.!;_--符号是指[]内部的一个范围,所以必须将其放在末尾。
  • 第二个正则表达式替换,,由,包围的空格(逗号,后面是空格)。
  • 第三个正则表达式将$替换为由空格包围的$ (空格前的美元符号)。在这个正则表达式中,您必须引用第二个组。
票数 3
EN

Stack Overflow用户

发布于 2018-09-07 18:43:11

使用r' (?=[-/?.!])|(?<=[-/$_]) '将模式re.sub替换为空字符串

代码语言:javascript
运行
复制
>>> lst = ["Tax pin number is 1 - 866 - 704 - 7388 .",
...              "No , emu is not protected in Victoria .",
...              "Find is to lose as construct is to _ _ _ _ _ _ .",
...              "$ 1,0 is equal to $ 1,0 ."]
>>> 
>>> def modify_answers(answers):
...     ptrn = re.compile(r' (?=[-/?.!])|(?<=[-/$_]) ')
...     return [ptrn.sub('', answer) for answer in answers]
... 
>>> 
>>> pprint(modify_answers(lst))
['Tax pin number is 1-866-704-7388.',
 'No , emu is not protected in Victoria.',
 'Find is to lose as construct is to ______.',
 '$1,0 is equal to $1,0.'] 
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52227716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档