1

I'm not to good in Regular Expressions. I have

string rgba = "[0.123,0.223,0.2,1]";

What would be the best way to covert it into double[] ?

Yuriy Faktorovich
68.9k14 gold badges107 silver badges150 bronze badges
asked Dec 26, 2010 at 0:25
1
  • assuming rgba strings have no spaces? (like [0.1, 1.0, 0.5, 1 ] Commented Dec 26, 2010 at 0:32

2 Answers 2

7
rgba.Replace("]", String.Empty)
 .Replace("[", String.Empty)
 .Split(',')
 .Select(double.Parse)
 .ToArray();

Or if you know that it will always start with [ and end with ]

rgba.Substring(1, rgba.Length - 2)
 .Split(',')
 .Select(double.Parse)
 .ToArray();

And if you don't like LINQ

Array.ConvertAll(rgba.Substring(1, rgba.Length - 2).Split(','), double.Parse);

Regex is quite expensive to use, and I wouldn't recommend it in this case.

answered Dec 26, 2010 at 0:28
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a regex with body:

\d+\.\d*

The regex will match one or more digits, then a single dot, then any number of digits.

answered Dec 26, 2010 at 0:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.