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
AlanPosted Sep 24, 2008, 6:22 PM
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 ServaPosted Sep 24, 2008, 7:09 PM
Sweet! It worked beautifully. I had to add a test for records that did not have .html, and then it was perfect.
Thank you!
Tony ServaPosted Sep 24, 2008, 5:39 PM
Sorry to be lame but where would I put the pace holder
Tony ServaPosted Sep 24, 2008, 5:26 PM
AlanPosted Sep 24, 2008, 5:23 PM
If you do need to use regex, then the following seems to be working OK with the new url:
Regex RE = new Regex(@"[^/]*(?=\.html.*$)");
AlanPosted Sep 24, 2008, 4:59 PM
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);
Tony ServaPosted Sep 24, 2008, 4:17 PM
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
AlanPosted Sep 23, 2008, 2:12 PM
Try:
Regex RE = new Regex(@"[^/]*$");
or to remove the .html as well:
Regex RE = new Regex(@"[^/]*(?=\.html$)");
Tony ServaPosted Sep 23, 2008, 2:11 PM
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.