How do I parse multiple numbers out of a string in C#? For instance, how do I get ALL the numbers out of this string: <3, 4, 4>
3 Answers
Use a regular expression with capturing groups.
\<(\d+), (\d+), (\d+)\>/
Something like the following perhaps:
Regex regex = new Regex(@"\<(\d+), (\d+), (\d+)\>/");
Match match = regex.Match(myString);
if (match.Success){
//Take matches from each capturing group here. match.Groups[n].Value;
}
else{
//No match
}
2 Comments
Conrad Frix
Presumably there might be N number of
\d between the bracketsAnirudh Ramanathan
@ConradFrix made that
\d+. ThanksSeems that you have numbers in a string separated by , So you can try this
string st = "3, 4, 4";
st = System.Text.RegularExpressions.Regex.Replace(st, " ", "");
//MessageBox.Show(st);
string[] ans = st.Split(',');
for (int i = 0; i < ans.Length; i++)
{
int num_At_i = Convert.ToInt32(ans[i]);
MessageBox.Show(num_At_i + "");
}