-2

I have a string like 'S10', 'S11' v.v How to split this to ['S','10'], ['S','11'] example:

import re
str = 'S10'
re.compile(...)
result = re.split(str)

result:

print(result)
// ['S','10']

resolved at How to split strings into text and number?

1 Answers1

0

This should do the trick:

I'm using capture groups using the circle brackets to match the alphabetical part to the first group and the numbers to the second group.

Code:

import re

str_data = 'S10'
exp = "(\w)(\d+)"
match = re.match(exp, str_data)
result = match.groups()

Output:

('S', '10')
Aasim Sani
  • 339
  • 2
  • 6