C# .NET - Regular Expression for pattern matching

Asked By abinav shankar on 01-Mar-12 07:02 AM
Hi I need a regular expression for the following Url
test.Abc.com/chennai-adyar
The regular expression should function as follows
1)when i simply give test.Abc.com/chennai-adyar it should display the contents in page number 1
2)when the next page button is clicked (example when page 3 is clicked) then the pattern should be matched and takes to the url
test.Abc.com/chennai-adyar_3 
All this should be handled in a single expression how to do it
[)ia6l0 iii replied to abinav shankar on 02-Mar-12 12:01 PM
Frankly speaking - I don't know why would one do such a thing using a Regular expression. 

Your problem seems to be something else. If you have absolute url's like the following, 
a) test.Abc.com/chennai-adyar
b) test.Abc.com/chennai-adyar_3 

And depending on the values after the domain, you need to route to a controller or something of that sort , that gives you the required data based on the url parameter.

I presume you are using ASP.Net, and if you are using v4.0, you can leverage the concept of Routing. 

You would do this in register routes method in global.asax.cs

public static void RegisterRoutes(RouteCollection routes)
{
            routes.Clear();
            // Turns off the unnecessary file exists check
            routes.RouteExistingFiles = true;
            //axd, sitemap 
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //ignore favicon requests from modern browsers.
            routes.IgnoreRoute("favicon.ico");
            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });
            //Ignore css
            routes.IgnoreRoute("{resource}.css/{*pathInfo}");
            //Ignore js
            routes.IgnoreRoute("{resource}.js/{*pathInfo}");
            //Ignore Content folder
            routes.IgnoreRoute("{folder}/{*pathInfo}", new { folder = "content" });
            //ignore all aspx routes.
            routes.IgnoreRoute("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
            //ignore all png requests.
            routes.IgnoreRoute("{*allpng}", new { allpng = @".*\.png(/.*)?" });
            
             routes.MapRoute(
              "allOtherRoutes",
             "{*url}",
              new { controller = "page", action = "render", id = "" }
            );
}


According to this example, you would use the page controller's render Action method to render the view accordingly to your page number. You would use the id parameter at the end, that would let you render the view accordingly. 

Hope this helps.