5

I want to split a string in C# that looks like

a : b : "c:d"

so that the resultant array will have

Array[0] = "a"

Array[1] = "b"

Array[2] = "c:d"

what regexp do I use to achieve the required result.

Many Thanks

2 Answers 2

4

If the delimiter colon is separated by whitespace, you can use \s to match the whitespace:

string example = "a : b : \"c:d\"";
string[] splits = Regex.Split(example, @"\s:\s");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Andy, that helps a great deal.
1

This seems to work in RegexBuddy for me

(\w+)\s:\s(\w+)\s:\s"(\w+:\w+)"

input

a : b : "c:d"

matched groups

  1. a
  2. b
  3. c:d

As always be careful and understand what the regex actually does. Don't just copy blindly. This matches word characters \w, spaces \s, etc. Consider what data your input will actually have in it!

3 Comments

Great, now he has two problems ;p
I got 99 problems but a regex ain't one.
I think there should be more wide solution with |..smth like: (\w+)\s:|:\s"(\w+:\w+)" ..or kind of this

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.