We have answer of your question!

100% solved queries, no empty question

Question: REGEX - Cut a text between number and letter


0

Is it possible to use a regex so as to obtain the following features ?

text       = "123abcd56EFG"
listWanted = ["123", "abcd", "56", "EFG"]

The idea is to cut the texte each time one digit is followed by one letter, or one letter is followed by one digit.

The solution thanks to the following answer

import re

pattern = r'(\d+|\D+)'

text  = "123abcd56EFG"
print(re.split(pattern, text))

text  = "abcd56EFG"
print(re.split(pattern, text))

This code will give...

['', '123', '', 'abcd', '', '56', '', 'EFG', '']
['', 'abcd', '', '56', '', 'EFG', '']
Question author Guest User: User1054158 | Source

Answer


1


You're going to want to do a split using: \d+|\D+ as your Regex.

--note that you need excape sequences to make the \ in your string, so the actual text entered will be: "\\d+|\\D+"

UNLESS, as noted in the comment below, you use a raw string, in which case it would be r"\d+|\D+" or r'\d+|\D+'

Answer author Nealon

Tickanswer.com is providing the only single recommended solution of the question REGEX - Cut a text between number and letter under the categories i.e text , python-3.x , . Our team of experts filter the best solution for you.

Related Search Queries:


You may also add your answer

Thanks for contributing an answer to Tick Answer!