How to write regex that searches for a dynamic amount of pairs?
Posted
by citronas
on Stack Overflow
See other posts from Stack Overflow
or by citronas
Published on 2010-03-05T20:27:10Z
Indexed on
2010/03/08
15:21 UTC
Read the original article
Hit count: 408
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?
© Stack Overflow or respective owner