tags:

views:

91

answers:

2

I want to extract MasterPage value directive from a view page. I want the fastest way to do that, taking into account a very big aspx pages, and a very small ones.

I think the best way is to do that with two stages:

  1. Extract the page directive section from the view (using Regex):
    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

  2. Then, extract the value inside MasterPageFile attribute.

The result needs to be: ~/Views/Shared/Site.Master.

Can I have help from someone to implement this? I badly want to use only regex, but I don't Regex expert.

Another thing, Do you think that Regex is the fastest way to do this?

+1  A: 

Here is a Regex that matches the Page directive and will have the value of the MasterPageFile attribute in group 1:

<%@\s?Page.*?MasterPageFile="([^"]+)".*?%>$

Why do you need it ? If you need to know the MasterPageFile when displaying the Page at runtime, there are easier and faster ways to do it.

driis
I'm build an automating test code that testing a lot of view pages. Do you think that this is the fastest way to do that? in a very big aspx file the regex may scan the all page, or is it optimized to look just for the first matched case (in this case the page directive block). Thanks!
stacker
If the .aspx is very big you will might want to only read the first _n_ lines, and run the regex on that. Then you have only little data for the Regex to operate on, and you will spend less time loading the pages. Disk IO tends to be slower than the CPU :-)
driis
The Regex.Match method will return the first match, then stop searching.
driis
Right, but it need to stop search after `%>` (the end of the page directive) EVEN if it don't find the `MasterPageFile` attribute.
stacker
The Regex I posted will not match anything not in a Page directive.
driis
+1  A: 
    string a = "<%@ Page Language=\"C#\" MasterPageFile=\"~/Views/Shared/Site.Master\" Inherits=\"System.Web.Mvc.ViewPage\" %>";

    Regex r = new Regex("<%@.*MasterPageFile=\"([^\"]*)\".*%>", RegexOptions.Compiled);
    Match m = r.Match(a);
    if (m.Success)
    {
       // what you want is in m.Groups[1]
    }

Groups is an array of strings containing the parts of the match. Groups[0] is the whole match, and the rest will be everything contained within parentheses in your regex. So above, I surrounded the part you want with parentheses.

Mark Synowiec
The search needs to be stop after the end of the page directive.
stacker
Thanks you for the help!
stacker