$text = '[2]-[37], [8]-[41], [11]-[24], [2]-[15]'; $arr = array_map(trim, explode(',',$text)); $arr = array_map(function($i) { $i = array_map(function ($j) { return trim($j, "[]"); }, explode('-', $i)); return $i; }, $arr); print_r($arr); 

as a result, there should be such an array:

 [ [2, 37], [8, 41], [11, 24], [2, 15] ] 

2 answers 2

To find all the natural numbers in a row and group them into pairs in Python 3:

 >>> import re >>> text = '[2]-[37], [8]-[41], [11]-[24], [2]-[15]' >>> list(zip(*[map(int, re.findall('\d+', text))]*2)) [(2, 37), (8, 41), (11, 24), (2, 15)] 
     text = '[2]-[37], [8]-[41], [11]-[24], [2]-[15]' def foo(text): return [ [int(w.strip('[]')) for w in s.strip().split('-')] for s in text.split(',') ] print(foo(text))