Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python正则表达式-在re.sub之前删除空格_Python_Regex_String - Fatal编程技术网

python正则表达式-在re.sub之前删除空格

python正则表达式-在re.sub之前删除空格,python,regex,string,Python,Regex,String,这是我的代码片段,这是基本的,我知道!我开始进入regex。当我运行下面的程序时,一切都正常,但我想知道如何缩小连字符和字符串替换之间的差距,例如,无论它在哪里表示一天的持续时间,例如,“八小时一天”现在改为“八小时一天”,我想关闭该空间。请帮忙 import re short_par = 'You must pace your work. What do I mean? I’m glad you asked that. We pace our work according to the ei

这是我的代码片段,这是基本的,我知道!我开始进入regex。当我运行下面的程序时,一切都正常,但我想知道如何缩小连字符和字符串替换之间的差距,例如,无论它在哪里表示一天的持续时间,例如,“八小时一天”现在改为“八小时一天”,我想关闭该空间。请帮忙

import re

short_par = 'You must pace your work. What do I mean? I’m glad you asked that. We pace our work according to the eight-hour workday. If you have twelve hours of work in your in-box, for example, you must compress that work into the eight-hour day. If you have one hour of work in your in-box, you must expand that work to fill the eight- hour day. That was a good question. Feel free to ask questions. Ask too many questions, however, and you may be let go.'

regex = re.compile('[hy]our')

print(regex.sub('BOOGIE', short_par))
您可以使用:


(?:(?请尊重SO社区并发布最简单的示例。不需要整个长
短\u par
字符串来说明您的问题,但我们必须水平滚动它才能找到
的“八小时”
place。我想你也可以使用
re.sub
做你想做的。你应该能够找到答案。非常有帮助,谢谢大家的见解。
(?:(?<=-) )?[hy]our