Given a list of strings like:
L = ['1759@1@83@0#1362@0.2600@25.7400@2.8600#1094@1@129.6@14.4',
'1356@0.4950@26.7300@2.9700',
'1354@1.78@35.244@3.916#1101@2@40@0#1108@2@30@0',
'1430@1@19.35@2.15#1431@3@245.62@60.29#1074@12@385.2@58.8#1109',
'1809@8@75.34@292.66#1816@4@24.56@95.44#1076@47@510.89@1110.61']
I need to extract all integers with length 4 between separators #
or @
, and also extract the first and last integers. No floats.
My solution is a bit overcomplicated - replace with space and then applied this solution:
pat = r'(?<!\S)\d{4}(?!\S)'
out = [re.findall(pat, re.sub('[#@]', ' ', x)) for x in L]
print (out)
"""
[['1759', '1362', '1094'],
['1356'],
['1354', '1101', '1108'],
['1430', '1431', '1074', '1109'],
['1809', '1816', '1076']]
"""
Is it possible to change the regex for not using re.sub
necessarily for replace? Is there another solution with better performance?