Search code examples
pythonpython-3.xregexstringpython-re

Split at number-letter


I want to split 'E100N05' into ['E100', 'N05'] , so split after a number become the letter N, E, S or W. The number of digits after a letter can be different.

import re
re.split('[NSEW$*]', 'E100N05')
Out[8]: ['', '100', '05']

What I want : ['E100','N05']

Which pattern should I use?


Solution

  • Try this:

    >>> re.split('(?!^)(?=[NSEW$])', 'E100N05')
    ['E100', 'N05']
    >>>