November 13th, 2011, 02:46 AM
Parse multilpl XML tags using regex
i need to parse a few tags (their value) from am XML.
this must be done by regex (don't ask me why )
i know the pattern needed for each tag using regex
string name = "(?<=<name>).+?(?=</name>)";
string id = "(?<=<id>).+?(?=</id>)";
string gender = "(?<=<gender>).+?(?=</gender>)";
i just don't know how to init the Regex object to handle all of them.
i can do:
private static readonly Regex rgx1 = new Regex(name);
private static readonly Regex rgx2 = new Regex(id);
private static readonly Regex rgx3r = new Regex(gender);
but I'm guessing that's a terrible waste....
so my question is - how to init a single Regex to handle multiple patterns?
and once i did it , how to extract the values from it?
I'm programming in c# if anyone need to know....
10x alot !
November 13th, 2011, 11:23 AM
This uses a back reference so that the closing tag matches the opening tag.
This regular expression will put the name of the tag into group 1 and the value of the tab into group two.
Comments on this post