Joseph Michael Pesch
VP Programming

RegEx: Take a URL and Replace the Page with New Page (isolate page from path and query)

by 14. July 2009 00:35

publicstaticstring ReplaceURLPageName(string URL, string PageNameExtension, string NewPageName)
{
 

// This method takes a URL and replaces the page (with given extension).
 
// Capture the root URL (i.e. everything up to the page
 
// Page is defined by first word with a period in it between slashes
 
string RootURL = "";  

foreach (Match m inRegex.Matches(URL, @"^.*/"))  
{
 
RootURL = m.ToString();
 
}
 

//Strip off everything before the page
 
URL = Regex.Replace(URL, @"^.*/", "");  

//Replace the current page name with new page name (keeping the querystring)
 
URL = RootURL + Regex.Replace(URL, "(.+)." + PageNameExtension, NewPageName);  
System.Diagnostics.Debug.WriteLine(URL);  


return URL;  }

Tags: ,

ASP.Net | C#

Comments are closed