Home > database >  I want to slice out substrings using regex
I want to slice out substrings using regex

Time:07-11

import re
str_ = "8983605653Sudanshu452365423256Shinde"
print(re.findall(r"\d{10}\B|[A-Za-z]{8}|\d{12}|[A-Za-z]{6}",str_))

current output

['8983605653', 'Sudanshu', '4523654232', 'Shinde']

Desired output

['8983605653', 'Sudanshu', '452365423256', 'Shinde']

CodePudding user response:

A regex find all on \d |\D should work here:

str_ = "8983605653Sudanshu452365423256Shinde"
matches = re.findall(r'\d |\D ', str_)
print(matches)  # ['8983605653', 'Sudanshu', '452365423256', 'Shinde']

The pattern used here alternatively finds all digit substrings, or all non digit substrings.

CodePudding user response:

Instead of using an alternation | you can use the matches with capture groups and then print the group values.

import re

str_ = "8983605653Sudanshu452365423256Shinde"
m = re.match(r"(\d{10})([A-Za-z]{8})(\d{12})([A-Za-z]{6})",str_)
if m:
    print(list(m.groups()))

Output

['8983605653', 'Sudanshu', '452365423256', 'Shinde']

See a Python demo.

  • Related