Imagine I have a block of text that contains words and numbers. I want to extract all the integers from the text. Thus, if the input is
"Now is the time for all good men to come 2 the aid of their country"
the correct output should be 2
(or {2}
).
Given the input
"1 is looking for 25 people to eat 3.14159"
the correct output should be {1, 25}
.
I have been working through a complex process with Characters[]
and Select[]
, but I think a much simpler solution must be possible.
Cases[ToExpression /@ StringCases[string, NumberString], _Integer]
$\endgroup$"Here's 1. Is it 1 or 1. ?"
$\endgroup$MatchQ[1., _Integer]
is not enough. $\endgroup$"Is 3/2 less than 2 or 3? How about -1, what can be done?"
$\endgroup$