3

I want to extract anything between two colon's (inclusive of the colon's) in an arbitrary input using C#. Given

String input = "a:one:b:two:c:three:d";

I want

{string[3]}
[0]: ":one:"
[1]: ":two:"
[2]: ":three:"

Using

String[ ] inverse = Regex.Split( input, ":.*?:" );

I get the opposite of what I want...

{string[4]}
[0]: "a"
[1]: "b"
[2]: "c"
[3]: "d"

How can I inverse this or is there something more suitable than Regex.Split in this situation?

3 Answers 3

1

How about :[^:]+: 1. Match a colon 2. Followed by any non colon character one or more times. 3. Followed by a colon.

To get the set of matches use MatchCollection matches = Regex.Matches(blah, ":[^:]+:"); instead of Regex.Split

Regex's are concise and powerfull but I find myself writting just as many comments as I would code when using them.

Sign up to request clarification or add additional context in comments.

3 Comments

This returns the same result as he was getting with his first attempt.
+1 @Matthew.. yup u can see I came to the same conclusion (to use Matches) in my accepting comment to Chris (the other Chris) above.. just a slightly diff pattern.. I'm with you on the pros/cons of regex's.. when I write lines of code that I know could be replaced with a regex it drives me nuts but then if I decide to use a regex to replace those lines of code that process tends to drives me nuts also.
decided to give you the answer as you actually provided a solution as opposed to Chris who gave a suggestion and presumably you hadn't seen the comment to know the Q was already answered.. thanks again.
0

How about you just use a regular split (on ':'), extract every second element, and then append the colons back on? I think you're overcomplicating this.

3 Comments

TBH that way is more complicated than the way I'm going about it now. I'd like to do the split + grouping in one operation not many
What you're doing isn't splitting, because splitting does exactly what it says. It slices right down the middle. What you're talking about is matching, and it's way too late where I am to come up with an elegant regex for this pattern.
haha accepted on the basis that your right on both counts.. 1. I need a match not a split... MatchCollection matches = Regex.Matches( input, ":.*?:"); will do the trick and 2. It's way too late to be coding AND|OR asking Qs on SO. Thanks for reminding me of both.
0

Try this regex:

^(\w*):|:\w:|:\w$

2 Comments

Hi bernesto.. I'm assuming you mean to use that regex with Split rather than Matches.. I tried both ways and neither worked or I am not using it as u intended.. either way I will stick with original ans but thanks for posting.
This is a split. Tested and working. Thanks for your comment.

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.