Hi,
Can anyone help me to understand how to take:,
http://www.mywebsite.com/news/local_news/Bullet-riddled_body_found_on_North_Side.html
And strip off everything before the last "/"
to result in:
Bullet-riddled_body_found_on_North_Side.html
The replace on the ".html" is something I know how to do and I think i need to use:
Regex RE = new
Regex("/",RegexOptions.RightToLeft);
Match theMatch = RE.Match(URL);
But his only returns the character "/".
How do you say delete everything before the first instance of the "/" character looking left to Right?
Thanks in advance.
Loading

AlanRank #669 · 3.6K reputationPosted Sep 24, 2008, 2:22 PM EDT
You don't need a placeholder because only one result is possible. Just do:
Regex RE = new Regex(@"[^/]*(?=\.html.*$)");
Match match = RE.Match(URL);
string myFileName = match.Value;
Tony Serva21 reputationPosted Sep 23, 2008, 10:11 AM EDT
Thanks to Phil Haack at http://forums.asp.net/t/551079.aspx
First is this:
string myUrl = URL;
Uri myUri = new Uri(myUrl);
string fileName = System.IO.Path.GetFileName(myUri.AbsolutePath);
And the second is this (which I am using)
Regex regex = new Regex(@"^.*/(?
Match match = regex.Match(URL);
string myFileName = match.Groups["myFileName"].Value;
But
1- Not all of the items in "URL" have filenames.
2- I need to remove the ".html"
So an if statement to test for the existence of the ".html" and then striping it out if it does should do it.
AlanRank #669 · 3.6K reputationPosted Sep 23, 2008, 10:12 AM EDT
Try:
Regex RE = new Regex(@"[^/]*$");
or to remove the .html as well:
Regex RE = new Regex(@"[^/]*(?=\.html$)");
Tony Serva21 reputationPosted Sep 24, 2008, 12:17 PM EDT
Thanks for the reply, but it did not actually work.
This is an example of the string:
http://www.mysite.com/news/local_news/One_dies_one_wounded_in_vehicle_shooting.html?c=y&viewAl
Regex
regex = new Regex(@"^.*/(?Result:
myFileName still = One_dies_one_wounded_in_vehicle_shooting.html?c=y&viewAl
I need it to look tike this:
One_dies_one_wounded_in_vehicle_shooting
Any ideas? I am reading through the O'Reilly Reg Ex book now
AlanRank #669 · 3.6K reputationPosted Sep 24, 2008, 12:59 PM EDT
Do you have to use regular expressions for this, Tony, as it's much easier to use traditional string parsing:
string url = "http://www.mysite.com/news/local_news/One_dies_one_wounded_in_vehicle_shooting.html?c=y&viewAl";
int index1 = url.LastIndexOf("/");
int index2 = url.LastIndexOf(".");
string pageName = url.Substring(index1 + 1, index2 - index1 - 1);
AlanRank #669 · 3.6K reputationPosted Sep 24, 2008, 1:23 PM EDT
If you do need to use regex, then the following seems to be working OK with the new url:
Regex RE = new Regex(@"[^/]*(?=\.html.*$)");
Tony Serva21 reputationPosted Sep 24, 2008, 1:26 PM EDT
Tony Serva21 reputationPosted Sep 24, 2008, 1:39 PM EDT
Sorry to be lame but where would I put the pace holder
Tony Serva21 reputationPosted Sep 24, 2008, 3:09 PM EDT
Sweet! It worked beautifully. I had to add a test for records that did not have .html, and then it was perfect.
Thank you!