1

i have this regular expression:

<%(?<begintag>[^>]+)%>(?<inner>(.+?))<%/(?<endtag>[^%]+)%>

what i want to achive is, check for equality of begintag and endtag .
consider this input :

<%layout%>
    <%pagetitle="test">
        <%products%>
            <div class="product">
                <img src="<%product-smallimage%>" class="thumbnail" alt="<%product-title%>"/>
                <a class="buy-link" href="<%product-url%>"><%product-title%></a>
            </div>
        <%/products%>

is there any way to check this ?

thanks in advance.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Ali Shokrollahi
  • 172
  • 1
  • 7
  • 2
    Warning - Regex is not a tool that can be used to correctly parse HTML. http://stackoverflow.com/a/1732454/12971 – Greg Apr 30 '12 at 15:58
  • i know. i don't want to parse html tags. i want to implement a template-based shop that can use different themes . i should replace <%products%> inner htmls with appropriate information. – Ali Shokrollahi Apr 30 '12 at 16:00

2 Answers2

3

As Igor said, you can capture the the name of the opening tag, then use a backreference to match a closing tag with the same name. There are a couple problems with his regex though; this one works:

(?s)<%(?<begintag>[^>]+)%>(.+?)<%/\k<begintag>%>

Here I've used a named backreference - \k<begintag> - instead of a numbered one - \1. It's always a good idea to use named backreferences if you're using named groups, but in .NET it's essential. In .NET, \1 does not match the same thing as (?<begintag>[^>]+) just because it's the first group.

I also used Singleline mode - (?s) - to allow the dot in (.+?) to match linefeeds.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
0

You can try using regex backreferences (\1 below):

<%(?<begintag>[^>]+)%>(?<inner>(.+?))<%/\1%>
Igor Korkhov
  • 8,283
  • 1
  • 26
  • 31