正则表达式用于在括号python之后删除空格

时间:2019-09-27 12:13:49

标签: python regex

我有如下字符串:

s1 = 'Hello , this is a [ test ] string with ( parenthesis ) .'

我正在尝试删除标点符号周围的空白,因此它应如下所示:

s1 = 'Hello, this is a [test] string with (parenthesis).'

我在这里找到了一些代码:How to strip whitespace from before but not after punctuation in python

req = re.sub(r'\s([?,.!"](?:\s|$))', r'\1', text)

我在正则表达式中添加了[]和),以包括在[]或)之后删除空格

 req = re.sub(r'\s([?,.!\])"](?:\s|$))', r'\1', text)

所以现在看起来像这样:

s1 = 'Hello, this is a [ test] string with ( parenthesis).'

现在,我一直在尝试对此进行调整,以在[或(之前删除空格,但我不知道如何。在使用正则表达式时我感到非常困惑。

我知道re.sub()用第一个参数替换了第二个参数(r'\ 1'),但我不明白(r'\ 1')的实际含义。

我们将不胜感激

欢呼

2 个答案:

答案 0 :(得分:2)

这可能有助于使用后向和前向。

import re

s1 = 'Hello , this is a [ test ] string with ( parenthesis ).'
#print(re.sub(r"(?<=\[|\()(.*?)(?=\)|\])", lambda x: x.group().strip(), s1))
print(re.sub(r'(\s([?,.!"]))|(?<=\[|\()(.*?)(?=\)|\])', lambda x: x.group().strip(), s1))

输出:

Hello, this is a [test] string with (parenthesis).

答案 1 :(得分:1)

一种方法是不捕获括号内开头和结尾的空间,即

 (parens start) some space (capture text) some space (parens close)
      |                          |                         |
   Group 1                   Group 2                    Group 3

匹配. or , preceded by space using alternation并将其捕获到单独的组中

([[({])\s*(.*?)\s*([\]\)\}])|\s+([,.])

enter image description here

替换为\1\2\3\4

Regex Demo