bcolletti
(Bruce Colletti)
July 11, 2015, 4:56am
1
From the string x = “DIMENSION: 17” I want to extract the integer and load it into n (in general, the string need not end in 17). This command works:
n = StringCases[x, "DIMENSION:" ~~ Whitespace ~~ (y : DigitCharacter ..) -> y] // First // ToExpression;
But there must be a much simpler way. What is it? Thankx.
Bruce
Is this simpler?
n = StringReplace[x, RegularExpression["\\D+"] -> " "] // ToExpression
which will handle any non-digit characters before and/or after the digits, but won’t handle several groups of digits.
girishav
(Girish Arabale)
July 11, 2015, 9:09am
3
You can also try–
StringSplit[x][[2]] // ToExpression
bcolletti
(Bruce Colletti)
July 12, 2015, 6:31pm
4
Girish, this is perfect, exactly what I sought. Thanks.
bcolletti
(Bruce Colletti)
July 12, 2015, 6:33pm
5
Bill, this is shorter and thus better – didn’t think of using RegularExpression. Girish provided the solution I hoped was out there, and both replies are useful. Thanks.