- Home /
Parsing a float from a string with text
Is there a way to find the value of a float or integer from a string which includes both numbers and characters, for example: "Music Volume: 100%"
Answer by hoy_smallfry · Aug 16, 2013 at 02:56 AM
If you need to extract the number string out of any string, such as "12"out of "12abc", or in this case "100" out of "Music Volume: 100%", you can use the Regex.Match
function, which is used to find the first sub-string that matches a regular expression (basically, a pattern) in strings.
import System.Text.RegularExpressions; // for Regex
//...
var theNumberString = Regex.Match(theOriginalString, "[+-]?\d*\.?\d*").Value;
The "[+-]?\d*\.?\d*"
is a Regex pattern for extracting a set of digits from where they start to where they stop. For more examples of Regex patterns, use this as a guide.
Once you have extracted a string that represents the number, you can then parse it out with the usual functions, such as Int32.Parse
, System.Convert.ToInt32
, etc.
Here's a little breakdown of what that Regex pattern means:
[]
is a range of symbols. With it, the pattern begins with any one symbol inside the brackets, in this case,+
or-
, to represent positive or negative numbers.?
says that the expression before it is optional. That basically means the pattern doesn't have to start out with+
or-
, such as "10" ins$$anonymous$$d of "+10".\d
refers to any number symbol, 0 through 9. It starts out with a\` to diffrentaiate it from the character symbol
d. -
*says to look for zero or more of the last expression. This lets the pattern match continue if there are more number symbols after the first, and stops when nothing else of the first matches, such as "123" out "123a456" -
.` refers to a dot symbol for decimal place, because.
has different Regex meaning. It's optional because of the?
after it, so that a number without a decimal place, like "1234", is considered valid.more numbers for after the decimal place, for when the decimal place is included in the match.
Hold on! Using the regex pattern returns an error: Unexpected characher 'd' I think it's because unity only recognizes the \ character for functions like \n or \t, is there a way to fix this?
Ah yeah, this is my bad. I was testing this in C#, where you can use the @ symbol before the string to avoid having to type out escape characters.
Since @ doesn't have the same meaning in Unityscript, I took it out but forgot to put the escape characters back in. Use this expanded form ins$$anonymous$$d:
"[\\+\\-]?\\d+\\.?\\d+"
I also replaced the * with + so that it looks for one or more ins$$anonymous$$d of zero or more, just for "correctness". It should work now. Sorry, I totally spaced out about it.
Additionally you can check if your pattern works at http://regexpal.com/