用'\ t'字符串替换空白字符

时间:2020-02-06 16:22:23

标签: regex python-3.x

我正在尝试用'\ t'字符串替换空格字符。文本文件如下所示:

255 255 255             white
  0   0   0             black
 47  79  79             dark slate gray
 47  79  79             DarkSlateGray
 47  79  79             DarkSlateGrey
105 105 105             dim gray

我的代码如下:

import re
with open('rgb.txt', 'r') as f:
    for line in f:
        print(re.sub(r'\s+', r'\\t', line))

上面的代码给出:

 255\t255\t255\twhite
 \t0\t0\t0\tblack
 \t47\t79\t79\tdark\tslate\tgray
 \t47\t79\t79\tDarkSlateGray
 \t47\t79\t79\tDarkSlateGrey
 105\t105\t105\tdim\tgray

但是,我只想替换第一个数字之后的空白,直到颜色名称为止。也不要介于两者之间。我想要的输出是:

 255\t255\t255\twhite
 0\t0\t0\tblack
 47\t79\t79\tdarkslategray
 47\t79\t79\tDarkSlateGray
 47\t79\t79\tDarkSlateGrey
 105\t105\t105\tdimgray

5 个答案:

答案 0 :(得分:2)

您可以在数字后立即匹配空格,这应该可以解决问题:

>>> txt = """255 255 255             white
...   0   0   0             black
...  47  79  79             dark slate gray
...  47  79  79             DarkSlateGray
...  47  79  79             DarkSlateGrey
... 105 105 105             dim gray"""
>>> for line in txt.split('\n'):
...     print(re.sub(r'[0-9]\s+', lambda m:m.group(0)[0]+r'\t', line))
...
255\t255\t255\twhite
  0\t0\t0\tblack
 47\t79\t79\tdark slate gray
 47\t79\t79\tDarkSlateGray
 47\t79\t79\tDarkSlateGrey
105\t105\t105\tdim gray

我无法找到一种快速的方法来忽略替换中的数字,因此我只是做了一个lambda来取匹配的数字并在其后附加了\t

答案 1 :(得分:1)

我建议使用嵌套的re.sub

re.sub(r'^[\d\s]+', lambda x: re.sub(r'\s+', '\t', x.group()), line)

要在开始时消除空格,请在运行正则表达式之前使用line.lstrip()

re.sub(r'^[\d\s]+', lambda x: re.sub(r'\s+', '\t', x.group()), line.lstrip())

第一个^[\d\s]+匹配行首的所有数字和空格,第二个re.sub用单个制表符替换空格字符串。

输出(对于没有.lstrip()的行):

255\t255\t255\twhite
\t0\t0\t0\tblack
\t47\t79\t79\tdark slate gray
\t47\t79\t79\tDarkSlateGray
\t47\t79\t79\tDarkSlateGrey
105\t105\t105\tdim gray

输出(对于带有.lstrip()的行):

255\t255\t255\twhite
0\t0\t0\tblack
47\t79\t79\tdark slate gray
47\t79\t79\tDarkSlateGray
47\t79\t79\tDarkSlateGrey
105\t105\t105\tdim gray

答案 2 :(得分:1)

使用正向查找 (?<=)

命名捕获组 (?P<>)

替代运算符 |

re.sub("(?P<a>(?<=\d)\s+)|(?P<b>\s+)", lambda m: "\t" if m.lastgroup == "a" else "", line)

输出:

255\t255\t255\twhite
0\t0\t0\tblack
47\t79\t79\tdarkslategray
47\t79\t79\tDarkSlateGray
47\t79\t79\tDarkSlateGrey
105\t105\t105\tdimgray

答案 3 :(得分:0)

我不熟悉python,无法在python中快速准确地回答问题,但是这里的javascript显示了regex实现。如果前三个参数始终是数字字符串,则可以按这种方式使用它。

var input = `255 255 255             white
  0   0   0             black
 47  79  79             dark slate gray
 47  79  79             DarkSlateGray
 47  79  79             DarkSlateGrey
105 105 105             dim gray`

var output = input.replace(/(\d+)\s+/g, '$1\\t')

console.log(output)

答案 4 :(得分:0)

您可以分两步进行操作:

import re
txt = """
255 255 255             white
  0   0   0             black
 47  79  79             dark slate gray
 47  79  79             DarkSlateGray
 47  79  79             DarkSlateGrey
105 105 105             dim gray
"""
for line in txt.split('\n'):
    line = re.sub(r'^\s+', '', line)  # remove leading spaces 
    print(regex.sub(r'(?<![a-zA-Z])(\s+)', r'\\t', line)) # change other spaces by \t when not preceded by a letter

输出:

255\t255\t255\twhite
0\t0\t0\tblack
47\t79\t79\tdark slate gray
47\t79\t79\tDarkSlateGray
47\t79\t79\tDarkSlateGrey
105\t105\t105\tdim gray