Extract Number from string into a list in Scala -


i have following string :

var mystr = "abc12ef4567gh90ijkl789" 

the size of list not fixed , contains number in between. want extract numbers , store them in form of list in manner:

list(12,4567,90,789) 

i tried solution mentioned here cannot extend case. want know if there faster or efficient solution instead of traversing string , extracting numbers 1 one using brute force ? also, string can arbitrary length.

it seems may collect numbers using

("""\d+""".r findallin mystr).tolist 

see scala demo. \d+ matches 1 or more digits, findallin searches multiple occurrences of pattern inside string (and un-anchors pattern partial matches found).

if prefer splitting approach, might use

mystr.split("\\d+").filter(_.nonempty).tolist 

see another demo. here, \d+ matches 1 or more non-digit chars, , these chunks used split on (texts between these chunks land in result). .filter(_.nonempty) remove empty items appear due matches @ start/end of string.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -