tags:

views:

67

answers:

1

Lets say a have a string such as this one:

string txt = "Lore ipsum {{abc|prop1=\"asd\";prop2=\"bcd\";}} asd lore ipsum";

The information I want to extract "abc" and pairs like ("prop1","asd") , ("prop3", "bcd") where each pair used a ; as delimeter.

Edit1: (based on MikeB's) code

Ah, getting close. I found out how to parse the following:

    string txt = "Lore ipsum {{abc|prop1=\"asd\";prop2=\"http:///www.foo.com?foo=asd\";prop3=\"asd\";prop4=\"asd\";prop5=\"asd\";prop6=\"asd\";}} asd";
    Regex r = new Regex("{{(?<single>([a-z0-9]*))\\|((?<pair>([a-z0-9]*=\"[a-z0-9.:/?=]*\";))*)}}", RegexOptions.Singleline | RegexOptions.IgnoreCase);
    Match m = r.Match(txt);
    if (m.Success)
    {
        Console.WriteLine(m.Groups["single"].Value);
        foreach (Capture cap in m.Groups["pair"].Captures)
        {
             Console.WriteLine(cap.Value);
        }
    }

Question 1: How must I adjust the regex to say 'each value of a pair in delimited by \" only? I added chars like '.',';' etc, but I can't think of any char that I want to permit. The other way around would be much nicer.

Question 2: How must I adjust this regex work with this thing here?

    string txt = "Lore ipsum {{abc|prop1=\"asd\";prop2=\"http:///www.foo.com?foo=asd\";prop3=\"asd\";prop4=\"asd\";prop5=\"asd\";prop6=\"asd\";}} asd lore ipsum {{aabc|prop1=\"asd\";prop2=\"http:///www.foo.com?foo=asd\";prop3=\"asd\";prop4=\"asd\";prop5=\"asd\";prop6=\"asd\";}}";

Therefore I'd probably try to get groups of {{...}} and use the other regex?

+2  A: 

I made some assumptions about how you're filtering, but I think the idea is pretty much there. Each pair is put into the pair group's collection of captures, and you can have any number of pairs.

        string txt = "Lore ipsum {{abc|prop1=\"asd\";prop2=\"bcd\";prop3=\"bbb\";}} asd lore ipsum";            
        Regex r = new Regex("{{(?<single>([a-z0-9]*))\\|((?<pair>((?<key>([a-z0-9]*))=\"(?<value>([a-z0-9]*))\";))*)}}", RegexOptions.Singleline | RegexOptions.IgnoreCase);
        Match m = r.Match(txt);            
        if (m.Success)
        {
            Console.WriteLine(m.Groups["single"].Value);
            foreach (Capture cap in m.Groups["pair"].Captures)
            {
                Console.WriteLine(cap.Value);                    
            }
            foreach (Capture cap in m.Groups["key"].Captures)
            {
                Console.WriteLine(cap.Value);
            }
            foreach (Capture cap in m.Groups["value"].Captures)
            {
                Console.WriteLine(cap.Value);
            }
        }
MikeB
The key/value split remains to be done, though.
RaphaelSP
I updated it to split by into key and value.
MikeB