Split string using integer as divisor
I have a rather long txt file filled with format lines {letter}{number}{letter}
. For example, the first few lines of my file:
A123E
G234W
R3L
H4562T
I am having a hard time finding the right pattern regex
to separate each line from alpha and numeric.
For example, on the first line, I need an array with the results:
print first_line[0] // A
print first_line[1] // 123
ptin first_line[2] // E
This seems to be regex
the way to go, but I'm still a beginner regex
. Can anyone help me in the right direction to point out how to do this?
Then I plan to iterate over each line and use the information as needed.
source to share
Divide into \d+
:
import re
re.split(r'(\d+)', line)
\d
is a character class corresponding to the numbers 0 through 9 and we want to match at least 1 of them. Placing around the capture group \d+
, re.split()
will comprise a coincidence output :
If the pattern uses parentheses for parentheses, then the text of all groups in the pattern is also returned as part of the resulting list.
Demo:
>>> import re
>>> re.split(r'(\d+)', 'A123E')
['A', '123', 'E']
source to share