search for two identical characters and filter them on python

Question:

how this task can be carried out is more optimized.
Various data come to me.
Example data : test/123, test , test/, test/123/
and I need to write the correct data to my database, but first I need to find it. The correct ones will be in the test/123/ format

and then divide them into variables
a = test
b = 123

Tell me how it can be done ?

the data can be of any size

Asked By: user20881617

||

Answers:

This answers for a data set of one or more letters ("test" in the example above) and one or more digits ("123" in the example above)

import re

data = 'test/123/'
m = re.match(r'(w+)/(d+)/', data)

if m:
    a = m[1]
    b = m[2]
    print(f'Found a = {a}, b = {b}')
else:
    print('no match')

For more tweaks of the pattern, refer e.g. to python’s Regular Expressions HOWTO

Answered By: Friedrich
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.