python - Getting empty string values when splitting alphanumeric values using regex -


i have long list string values this:

ab65 

i want split the letters numbers, when this:

re.split('([a-z]+)([0-9]+)', 'ab65')  

i following empty string values:

['', 'ab', '65', ''] 

how values this: ['ab', '65'] thank help.

re.split meant text between regex matches; need re.findall instead:

>>> re.findall('([a-z]+)([0-9]+)', 'ab65')  [('ab', '65')] 

this still doesn't quite work, because regex matches entire string 'ab65' , contains 2 capturing groups. need regex matches either letters or numbers (so separate them |), , uses non-capturing groups (so use (?:…)):

>>> re.findall('(?:[a-z]+)|(?:[0-9]+)', 'ab65')  ['ab', '65'] 

in fact, in simple case, parentheses aren't necessary:

>>> re.findall('[a-z]+|[0-9]+', 'ab65')  ['ab', '65'] 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -