1

I have a string that contain the following text:
[l=9;f=0;r=5;p=2]
There may be more than one:
[l=9;f=0;r=5;p=2];[l=9;f=0;r=6;p=2]

I want to get the array of strings(for second example):

[l=9;f=0;r=5;p=2]
[l=9;f=0;r=6;p=2]

I tried this but it split not correctly:

Regex.Split(seat, "(?=])");

PS. regexp not mandatory.

3
  • U need to make it with Regex syntax? show us result you want to get Commented Aug 28, 2012 at 10:51
  • @harry180: the result shows in question, I want to get array Commented Aug 28, 2012 at 10:52
  • 1 dimensional array or 2 dimensional array? or maybe you want a Dictionary<string,int>? Commented Aug 28, 2012 at 11:00

6 Answers 6

8
string input = "[l=9;f=0;r=5;p=2];[l=9;f=0;r=6;p=2]";
var list = Regex.Matches(input, @"\[.+?\]")
            .Cast<Match>()
            .Select(m => m.Value)
            .ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

@chridam: Yes, of course. Just SO say after 5 minutes, please.
3

The below pattern might help you use the Split option of Regex

string input = "[l=9;f=0;r=5;p=2];[l=9;f=0;r=6;p=2]";
string pattern = @"(?<=\]);";
Regex regex = new Regex(pattern);
string[] data = regex.Split(input);

Comments

0
"[l=9;f=0;r=5;p=2];[l=9;f=0;r=6;p=2]".Split(new string[] { "];" }, StringSplitOptions.None)

And then append "]" back to each item in the array...

Ugly but should work.

3 Comments

You should mention that then OP will need to add ] for his elements inside array
@niaher: no the first element will be equal [l=9;f=0;r=5;p=2
You're right, so appending the "]" should do the trick.. Not the most elegant solution of course.
0

Why don't you use regex capturing?

The pattern \[l=(\d);f=(\d);r=(\d);p=(\d)\] will capture the values in each array.

Example:

private static IEnumerable<dynamic> Match(string text)
{
    return Regex.Matches(text, @"\[l=(\d);f=(\d);r=(\d);p=(\d)\]")
        .Cast<Match>()
        .Where(m => m.Success)
        .Select(m => new { l = m.Groups[1].Value, f = m.Groups[2].Value, r = m.Groups[3].Value, p = m.Groups[4].Value });
}

static void Main(string[] args)
{
    foreach (var result in Match("[l=9;f=0;r=5;p=2];[l=9;f=0;r=6;p=2]"))
        Console.Out.WriteLine("Setting: {0}, {1}, {2}, {3}", result.l, result.f, result.r, result.p);

    foreach (var result in Match("[l=9;f=0;r=5;p=2]"))
        Console.Out.WriteLine("Setting: {0}, {1}, {2}, {3}", result.l, result.f, result.r, result.p);
}

Comments

0

Here's a LINQ approach:

Func<string, string> process = s =>
    String.Format("[{0}]",
        String.Join("];[",
            s
                .Split('[')
                .Select(x => x.Split(']'))
                .SelectMany(x => x)
                .Where(x => x != "" && x !=";")));

At the very least it works. :-)

Use it like this:

var result = process("[l=9;f=0;r=5;p=2];[l=9;f=0;r=6;p=2]");

Comments

0

In your code:

Regex.Split(seat, "(?=])");

Seems like you're just missing the actual semicolon, and a backslash for the brakcet, from your regex. This works:

 string[] data = Regex.Split(seat, ";(?=\\[)");

Comments

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.