Split string when a number is followed by a letter

Question:

I am dealing with a function that returns variable names with no space between them. Do you have any suggestions for fixing the issue, for example using regex?

Example output: JS876383JS782221JS7721740, whose component should be separated into something like [JS876383, JS782221 JS7721740].

Asked By: albus_c

||

Answers:

re.split(r"(?<=d)(?=[A-Za-z])", "JS876383JS782221JS7721740")
# ['JS876383', 'JS782221', 'JS7721740']

The (?<=d) is a lookbehind for any digit, and the (?=[A-Za-z]) lookaheads to any letter.

Answered By: Eric Jin

maybe this?

string = "JS876383JS782221JS7721740"
t = string.split('JS')[1:]
ind = 0
while ind < len(t):
    t[ind] = 'JS' + t[ind]
    ind+=1

print(t)
Answered By: Rickyxrc
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.