0

If I have a string in this format:

string placeholder = "[[Ford:Focus(blue)]]";

What regex could I use to populate the three variables below?

string make = ?
string model = ?
string colour = ?

Any ideas?

Thanks,

1 Answer 1

1
string input = "[[Ford:Focus(blue)]]";
var parts = Regex.Matches(input, @"\w+").Cast<Match>().Select(x => x.Value).ToList();

var make = parts[0];
....

OR

var match = Regex.Match(input, @"\[\[(\w+):(\w+)\((\w+)\)\]\]");
var make = match.Groups[1].Value;
var model = match.Groups[2].Value;
....
Sign up to request clarification or add additional context in comments.

5 Comments

Great...what though if colour was optional and it wasn't specified...is there a way in the regex to set it to empty string?
I suppose you could then use \w* instead of \w+.
@thegunner For the first regex, you can check the Length of parts, for the 2nd, you should give a sample input
If the "colour" part has a space it doesn't get pick up..e.g."light blue". Any suggestions?
@thegunner then replace \w+ with [a-zA-Z ]+

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.