正则表达式:在数字后跟字母后添加 space

Regex: Add space after a number when followed by letter

在一组数字之后,我想在字符串中添加一个 space。例如,以下字符串应在数字后添加 space:

Before                           After
"0ABCD TECHNOLOGIES SERVICES"    "0 ABCD TECHNOLOGIES SERVICES"
"ABCD0 TECHNOLOGIES SERVICES"    "ABCD 0 TECHNOLOGIES SERVICES"

"ABCD 0TECHNOLOGIES SERVICES"    "ABCD 0 TECHNOLOGIES SERVICES"
"ABCD TECHNOLOGIES0 SERVICES"    "ABCD TECHNOLOGIES 0 SERVICES"

"ABCD TECHNOLOGIES 0SERVICES"    "ABCD TECHNOLOGIES 0 SERVICES"
"ABCD TECHNOLOGIES SERVICES0"    "ABCD TECHNOLOGIES SERVICES 0"

我一直在尝试使用 Python 中的正则表达式,如下所示:

text= re.sub(r'([0-9]+)?([A-Za-z]+)?([0-9]+)?',
             r'  ',
             text,
             0,
             re.IGNORECASE)

使用之前的代码,我得到了不需要的 spaces,它们影响了其他正则表达式转换:

"0 abcd     technologies     services   "

如何在不添加不需要的 space 的情况下在字符串中添加 space?

您可以使用

re.sub(r'(?<=\d)(?=[^\d\s])|(?<=[^\d\s])(?=\d)', ' ', text)

参见regex demo

图案详情

  • (?<=\d)(?=[^\d\s]) - 数字和字符之间的位置,而不是数字和空格
  • | - 或
  • (?<=[^\d\s])(?=\d) - 除了数字和空格之外的字符和数字之间的位置。

Python test:

import re
tests = ['0ABCD TECHNOLOGIES SERVICES',
'ABCD0 TECHNOLOGIES SERVICES',
'ABCD 0TECHNOLOGIES SERVICES',
'ABCD TECHNOLOGIES0 SERVICES',
'ABCD TECHNOLOGIES 0SERVICES',
'ABCD TECHNOLOGIES SERVICES0']

rx = re.compile(r'(?<=\d)(?=[^\d\s])|(?<=[^\d\s])(?=\d)')

for test in tests:
    print(rx.sub(' ', test))

输出:

0 ABCD TECHNOLOGIES SERVICES
ABCD 0 TECHNOLOGIES SERVICES
ABCD 0 TECHNOLOGIES SERVICES
ABCD TECHNOLOGIES 0 SERVICES
ABCD TECHNOLOGIES 0 SERVICES
ABCD TECHNOLOGIES SERVICES 0