views:

78

answers:

3

A little stuck here. I have a simple question I guess.

Given the following input:

 content {c:comment comment}this is actual content{c:comment etc} content

I need a way to get the content and comments seperated, but I need to now the order of them. So a simple regex doesn't work.

I want to get this:

 content
 {c:comment comment}
 this is actual content
 {c:comment etc}
 content

Somebody a clue?

A: 
string s = "content {c:comment comment}this is actual content{c:comment etc} content";
var split = Regex.Split(s, @"\{c:(?<comment>[^\}]*)\}");

NOTE: when the regex of a split has a capturing like (?<comment> ) it also gets returned.

Nestor
Ended up with: List<string> Regex.Split(input "{|}"). comments kept the "c:this is a comment" "c:" so I could recognize if it was a comment. Didn't think of Regex it's Split method. So this helped.
Peterdk
A: 

The expression (.*?)\s*{c:\s*(.*?)}\s* gave me a string[] with the items: 'content', 'comment comment', 'this is actual content', 'comment etc', 'content'

Maybe that's close to what you're looking for.

Kivin
+2  A: 

As Artelius suggested:

Regex.Replace(
Regex.Replace(input, "({)", @"\r\n$1"),
                     "(})", @"$1\r\n");
Rubens Farias
No, because you want a newline BEFORE { but AFTER }.
Artelius
you're right, fixed
Rubens Farias