简体   繁体   中英

Insert space between specific characters but not if followed by specific characters regex

Using python regex, I wish to insert a space between alpha characters and numerals (alpha will always preceed numeral), but not between (numerals and hyphens) or between (numerals and underscores).

Ideally, I'd like it to replace all such examples on the line (see the 3rd sample string, below) , but even just doing the first one is great.

I've gotten this far:

import re
item = "Bob Ro1-1 Fred"
txt = re.sub(r"(.*)(\d)", r"\1 \2", item)
print(txt) #prints Bob Ro1 -1 Fred (DESIRED WOULD BE Bob Ro 1-1 Fred)

I've tried sticking a ? in various places to ungreedify the search, but haven't yet found the magic.

Sample strings: Original ==> Desired output
1. "Bob Ro1 Sam cl3" ==> "Bob Ro 1 Sam cl 3"
2. "Some Guy ro1-1 Sam" ==> "Some Guy ro 1-1 Sam"
3. "ribbet ribbit ro3_2 bob wow cl1-3" ==> "ribbit ribbit ro 3_2 bow wow cl 1-3"

You may use

re.sub(r'([^\W\d_])(\d)', r'\1 \2', s)

See the regex demo

A variation using lookarounds:

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

The ([^\\W\\d_])(\\d) regex matches and captures into Group 1 any single letter and into Group 2 the next digit. Then, the \\1 \\2 replacement pattern inserts the letter in Group 1, a space, and the digit in Group 2 into the resulting string.

The (?<=[^\\W\\d_])(?=\\d) matches a location in between a letter and a digit, and thus, the replacement string only contains a space.

See the Python demo :

import re
strs = [ 'Bob Ro1-1 Fred', 'Bob Ro1 Sam cl3', 'Some Guy ro1-1 Sam', 'ribbet ribbit ro3_2 bob wow cl1-3' ]
rx = re.compile(r'([^\W\d_])(\d)')
for s in strs:
    print(re.sub(r'([^\W\d_])(\d)', r'\1 \2', s))
    print(re.sub(r'(?<=[^\W\d_])(?=\d)', ' ', s))

Output:

Bob Ro 1-1 Fred
Bob Ro 1-1 Fred
Bob Ro 1 Sam cl 3
Bob Ro 1 Sam cl 3
Some Guy ro 1-1 Sam
Some Guy ro 1-1 Sam
ribbet ribbit ro 3_2 bob wow cl 1-3
ribbet ribbit ro 3_2 bob wow cl 1-3

You need a look ahead following a look behind:

(?<=[a-zA-Z])(?=[0-9])

The code should be re.sub(r"(?<=[a-zA-Z])(?=[0-9])", r" ", item)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM