Either regex or in python I would like to only extract the numbers or integers before and after the delimiter x
input = "103ABCD:/store125x125_line"
input2 = "/line_125x1875_2x1"
cline= (input.split('x')[1]).split('_')[0]
xline= ((input.split('x')[0]).split('/')[1]).split('store')[1]
print(cline,xline)
Expect Results: 125 125
and 125 1875
Is there a simpler method which can be used for both input and input2 scenario?
CodePudding user response:
You could use a regular expression to search for the values.
import re
matcher = re.compile(r"(\d )x(\d )")
input1 = "103ABCD:/store125x125_line"
input2 = "/line_125x1875_2x1"
if match := matcher.search(input1):
cline, xline = match.groups()
print(cline, xline)
if match := matcher.search(input2):
cline, xline = match.groups()
print(cline, xline)
Outputs:
125 125
125 1875
CodePudding user response:
We can use a regex based approach here:
inp = ["103ABCD:/store125x125_line", "/line_125x1875_2x1"]
for i in inp:
print(i)
print(re.findall(r'(?:store|line)_?(\d )x(\d )', i)[0])
This prints:
103ABCD:/store125x125_line
('125', '125')
/line_125x1875_2x1
('125', '1875')