tags:

views:

116

answers:

4

Hi, if i have a string of text like below, how can i collect the contents of the brackets in a collection in c# even if it goes over line breaks?

eg...

string s = "test [4df] test [5yu] test [6nf]";

should give me..

collection[0] = 4df

collection[1] = 5yu

collection[2] = 6nf

+1  A: 
Regex regex = new Regex(@"\[[^\]]+\]", RegexOptions.Multiline);
Thomas Levesque
+1  A: 

The key is to correctly escape the special characters used in regular expressions, for example you can match a [ character this way: @"\["

fviktor
+2  A: 

You can do this with regular expressions, and a bit of Linq.

    string s = "test [4df] test [5y" + Environment.NewLine + "u] test [6nf]";

    ICollection<string> matches =
        Regex.Matches(s.Replace(Environment.NewLine, ""), @"\[([^]]*)\]")
            .Cast<Match>()
            .Select(x => x.Groups[1].Value)
            .ToList();

    foreach (string match in matches)
        Console.WriteLine(match);

Output:

4df
5yu
6nf

Here's what the regular expression means:

\[   : Match a literal [
(    : Start a new group, match.Groups[1]
[^]] : Match any character except ]
*    : 0 or more of the above
)    : Close the group
\]   : Literal ]
Mark Byers
A: 
Regex rx = new Regex(@"\[.+?\]");
var collection = rx.Matches(s);

You will need to trim the square brackets off, the important part is the lazy operator.

Paul Creasey
Won't work over multiple lines, even if you specify RegexOptions.Multiline (I know, I tried ;))
Thomas Levesque
Works accross multiples line fine for me
Paul Creasey
You could use Regex.SingleLine - this almost works except it captures the \n's too. In fact this would probably even perform slightly better performing than the solution I presented since it only needs to Replace on the captured parts of the string rather than the whole string.
Mark Byers