Home > Software engineering >  Split string when a number is followed by a letter
Split string when a number is followed by a letter

Time:08-18

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].

CodePudding user response:

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.

CodePudding user response:

maybe this?

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

print(t)
  • Related