Page 1 of 1

Regex-nested tags

Posted: Mon Nov 13, 2006 6:44 am
by Mira
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


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: 

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

This is my sample code
***********************

Code: Select all

                        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


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Nov 14, 2006 8:28 pm
by Ambush Commander
You probably want to use XSLT to do the job: recursive regexps usually never turn out very well.