# Search for individual numbers in a variable

Hi Folks,

sorry for stealing you time, but is it possible to search in a variable for specific numbers?

the variable xstr looks like this:

+cops: (2,“T-Mobile A”, “TMO A”, “23203”,0),(3,“one”,“one”,“23205”),(1,“A1”,“A1”,“23201”,0),(0,1,3,4),(0,1,2)

OK

Is it possible to search for all numbers with 5 Digits? - That means the result should be: 23203,23205,23201 - and nothing else?

It is not possible to make it static, because there can also be only one or two or even more numbers…

Thanks for your help!

Best regards,

Stefan

something like this:

``````set theList to {{2, "T-Mobile A", "TMO A", "23203", 0}, {3, "one", "one", "23205"}, {1, "A1", "A1", "23201", 0}, {}, {0, 1, 3, 4}, {0, 1, 2}}
set {TID, text item delimiters} to {text item delimiters, ","}
set theList to theList as text -- transform the lists
set theList to text items of theList -- into one single list
set text item delimiters to TID
set theNumbers to {}
repeat with i in theList
try
tell (i as integer) to if (count (it as string)) is 5 then set end of theNumbers to it
end try
end repeat
``````

Alternatively:

``````set test to "+cops: (2,\"T-Mobile A\", \"TMO A\", \"23203\",0),(3,\"one\",\"one\",\"23205\"),(1,\"A1\",\"A1\",\"23201\",0),,(0,1,3,4),(0,1,2)"

do shell script "echo " & quoted form of test & " | /usr/bin/grep -o '[^0-9][0-9]\\{5\\}[^0-9]' | /usr/bin/grep -o '[0-9]\\{5\\}'"
set fiveDigitsList to paragraphs of result
``````

The first grep pattern finds a character that is not a number, which is followed by five numbers in a row, after which there is another character that is not a number. The second pattern finds five numbers in a row.

If I may suggest an alternative formulation, this one is more than 3 times as fast (if you have to do this often it might matter) according to Lotsa.

set L to {{2, “T-Mobile A”, “TMO A”, “23203”, 0}, {3, “one”, “one”, “23205”}, {1, “A1”, “A1”, “23201”, 0}, {}, {0, 1, 3, 4}, {0, 1, 2}}

set {TID, text item delimiters} to {text item delimiters, “,”}
set L to text items of (L as text) – into one single list
set text item delimiters to TID
set N to {}
repeat with i in L
if (count i) = 5 then
try
set N’s end to i as integer
end try
end if
end repeat

Hi Folks,

you are great - thanks a lot…

Best Regards,

Stefan

Great, Adam, I didn’t care about speed