regex for Nested tags

View: New views
2 Messages — Rating Filter:   Alert me  

regex for Nested tags

by mira :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am writing the match case for xml tags inorder to show it as html in a div container.
where in an xml file i will have emphasis tags for bold,italic,undeline etc.
I want to know how to write match the xml tags if it contains nested emphasis tags.
for example when i match like if(Regex.IsMatch(l,"(<Emphasis Type=\"Italic\">.+?</Emphasis>)",RegexOptions.IgnoreCase))
I will get problem when I have the xml as:

<Emphasis Type=\"Italic\">
      <Emphasis Type=\"bold\">sdjskdjskdj</Emphasis>
       sjdksjds ksjd kdjs djsd ksdj kdjskd jdsd
<Emphasis>  
 
This is my sample code
***********************

                         if(Regex.IsMatch(l,"(<Emphasis Type=\"Italic\">.+?</Emphasis>)",RegexOptions.IgnoreCase))
    {
     Regex r =new Regex("(<Emphasis Type=\"Italic\">.+?</Emphasis>)",RegexOptions.IgnoreCase);
     Match m =r.Match(l);
     Group g;
     while(m.Success)
     {
      g=m.Groups[0];
      l =l.Remove(g.Index,g.Length);
      string k=g.Value;
      k=Regex.Replace(k,"<Emphasis Type=\"Italic\">","",RegexOptions.IgnoreCase);
      k=Regex.Replace(k,"</Emphasis>","",RegexOptions.IgnoreCase);
      l =l.Insert(g.Index,"<EM>"+k+"</EM>");
      m=r.Match(l);
     }
    }
                   
    if(Regex.IsMatch(l,"(<Emphasis Type=\"Underline\">.+?</Emphasis>)",RegexOptions.IgnoreCase))
    {
     Regex r =new Regex("(<Emphasis Type=\"Underline\">.+?</Emphasis>)",RegexOptions.IgnoreCase);
     Match m =r.Match(l);
     Group g;
     while(m.Success)
     {
      g=m.Groups[0];
      l =l.Remove(g.Index,g.Length);
      string k=g.Value;
      k=Regex.Replace(k,"<Emphasis Type=\"Underline\">","",RegexOptions.IgnoreCase);
      k=Regex.Replace(k,"</Emphasis>","",RegexOptions.IgnoreCase);
      l =l.Insert(g.Index,"<U>"+k+"</U>");
      m=r.Match(l);
     }
    }

can any one help me in writing generalised Regex for N number of nested emphasis tags.

 

Thanks


Re: regex for Nested tags

by StevenLevithan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

See my blog post on matching nested constructs using regular expressions here: http://badassery.blogspot.com/2006/03/regex-recursion-without-balancing.html

If you still need help after reading that, let me know.

mira wrote:
I am writing the match case for xml tags inorder to show it as html in a div container.
where in an xml file i will have emphasis tags for bold,italic,undeline etc.
I want to know how to write match the xml tags if it contains nested emphasis tags.
for example when i match like if(Regex.IsMatch(l,"(<Emphasis Type=\"Italic\">.+?</Emphasis>)",RegexOptions.IgnoreCase))
I will get problem when I have the xml as:

<Emphasis Type=\"Italic\">
      <Emphasis Type=\"bold\">sdjskdjskdj</Emphasis>
       sjdksjds ksjd kdjs djsd ksdj kdjskd jdsd
<Emphasis>