Assuming the text can be streamed in reverse order (a reasonable assumption since strings in most languages are backed by an array of characters with O(1)
access), construct the number by reading the text backwards until you hit a character that is not a digit or the text has been consumed entirely.
numDigits = 0
number = 0
while(numDigits <> length and characterAt[length - numDigits] is a digit)
number = number + (parseCharacterAt[length - numDigits] * (10 ^ numDigits))
numDigits = numDigits + 1
end while
if(numDigits is 0)
Error ("No digits at the end")
else return number
Note: (10 ^ numDigits) can be trivially optimized with another variable.