Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to extract the number from an alphanumeric string. This is what I tried:

StringTake["thiru3", {6, 6}]

The result I got is 3, but it is still a String, which I determined by evaluating:

NumberQ[StringTake["thiru3", {6, 6}]]

which returns False.

share|improve this question
@Mr.Wizard. I don't think it's a duplicate. This is about extracting a simple integer, not about a real an encoded in a string. – m_goldberg Feb 27 '13 at 6:04
 StringCases["thiru3", x : NumberString :> ToExpression[x]]
 (* {3} *)
 (* 3 *)
 {NumberQ[%], Head[%]}
 (* {True, Integer} *)
share|improve this answer
As noted by ssch in Chris's answer, FromDigits[] might be better to use than ToExpression[]. – J. M. Apr 4 '13 at 14:55
NumberString will match real number strings and FromDigits will balk; ToExpression will convert them. I would say NumberString & ToExpression for real numbers, or, for integers only, DigitCharacter.. and FromDigits. You won't get messages in either case, as in ssch's comment, because FromDigits is called only when there is a match. – Michael E2 Oct 16 '13 at 12:09

Try to use ToExpression, like this

ToExpression[StringTake["thiru3", {6, 6}]]

If you check it with Head[%] it confirms that it is an Integer

share|improve this answer

Alternatively, and for multiple numbers:-

Map[FromDigits, Select[Characters["thiru37"], DigitQ]]

{3, 7}


ToExpression@StringCases["thiru37", DigitCharacter]

{3, 7}

share|improve this answer
I think FromDigits is the best way since you will immediatly get a message printed in case it didn't work, that you can Check for if need be – ssch Dec 22 '12 at 19:23

Here is one compact option



share|improve this answer
Nice - where the heck did you get that from? – Yves Klett Oct 16 '13 at 15:03
Ahh.. I was looking for a faster way to make conversions from string to numbers, and found it here – Murta Oct 16 '13 at 22:15

Like NumberString, DigitCharacter can be used to find numbers in a string. Use relative positioning in the string (like EndOfString) to localize your digits and ignore other numbers possibly appearing at other positions:

First@StringCases["0thi12ru3", (n:DigitCharacter~~EndOfString) :> ToExpression@n]

(* ==> 3 *)

This searches for exactly one number character right before the end of string. If the terminal number could have more than one digits, use DigitCharacter.. instead.

share|improve this answer
s = "thiru3";
r = First@StringCases[s, RegularExpression["\\d+"]];
z = Integer`parseInt[r]

Mathematica graphics

share|improve this answer
bbbut... why not ToExpression@r? – R. M. Dec 22 '12 at 23:07
@rm-rf The sarcasm character doesn't display in your browser I guess. – Sjoerd C. de Vries Dec 22 '12 at 23:13
wish I could upvote twice. what an economical solution – acl Dec 23 '12 at 0:50


str = "thiru37aa2er45"


StringCases[str, DigitCharacter ..]


{"37", "2", "45"}

StringCases[str, DigitCharacter .]


{"3", "7", "2", "4", "5"}

They can be converted to expressions using ToExpression

share|improve this answer
StringCases["thiru37aa2er45", x : DigitCharacter .. :> FromDigits[x]] – Mr.Wizard Oct 16 '13 at 11:15
@Mr.Wizard thank you...all in one step – ubpdqn Oct 16 '13 at 11:40

Try this.

Read[StringToStream[StringTake["thiru3", {6, 6}]]]
share|improve this answer

Can be so

ToExpression[StringCases["thiru3", DigitCharacter ..]][[1]]


  StringCases["thiru3", "thiru" ~~ (x : DigitCharacter ..) -> x]][[1]]


ToExpression[StringCases["thiru3", RegularExpression["\\d+"]][[1]]]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.