3

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 3

2

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Presumably there might be N number of \d between the brackets
@ConradFrix made that \d+. Thanks
1

Seems 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 + "");
        }

Comments

0

Did you try this? Basic Regex: [0-9]+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.