millerthegorilla
asked on
How do I write a regular expression to trim a complex path name
I have a string with a path name eg. 'c:\microsoft\windows\shar ed'. I want to pass this string to a reg exp and get the string 'c:\microsoft\windows' back from it. How do I do this?
I got a good answer for this already. However the regexp that I was given doesn't work on the type of path that name that I'm using. The answer I got was :
String sourcestring = @"c:\microsoft\windows\sha red";
String matchpattern = @"(.*)\\.*";
String replacementpattern = @"$1";
string bob = Regex.Replace(sourcestring , matchpattern, replacementpattern);
which does in fact return c:\microsoft\windows to bob.
however, the path I'm using is something like the following: c:\.....\siteview\1.0.0.0_ _9f4da0011 6c38ec5
I need to trim the final folder of the path using a reg exp.
I got a good answer for this already. However the regexp that I was given doesn't work on the type of path that name that I'm using. The answer I got was :
String sourcestring = @"c:\microsoft\windows\sha
String matchpattern = @"(.*)\\.*";
String replacementpattern = @"$1";
string bob = Regex.Replace(sourcestring
which does in fact return c:\microsoft\windows to bob.
however, the path I'm using is something like the following: c:\.....\siteview\1.0.0.0_
I need to trim the final folder of the path using a reg exp.
Please show what result you want and what result you get from the provided code. Thanks.
ASKER
I want back 'c:\....\siteview'
the provide code just returns the same string.
the provide code just returns the same string.
Show the C# code you are using with your source string included so that I can test here. In the truncated example I don't see the issue:
Raw Match Pattern:
(.*)\\.*
Raw Replace Pattern:
$1
C#.NET Code Example:
using System;
using System.Text.RegularExpressions;
namespace myapp
{
class Class1
{
static void Main(string[] args)
{
String sourcestring = "source string to match with pattern";
String matchpattern = @"(.*)\\.*";
String replacementpattern = @"$1";
Console.WriteLine( Regex.Replace(sourcestring,matchpattern,replacementpattern));
}
}
}
$sourcestring after replacement:
c:\.....\siteview
ASKER
here you go. the variable location contains:
http://spdev/personal/administrator/_wpresources/SiteViewV4/1.0.0.0__9f4da00116c38ec5/
imageName is 'image1' etc
http://spdev/personal/administrator/_wpresources/SiteViewV4/1.0.0.0__9f4da00116c38ec5/
imageName is 'image1' etc
private string getImageLocation(string imageName)
{
String matchpattern = @"(.*)\\.*";
String replacementpattern = @"$1";
string directory = @"images/";
string rval = Regex.Replace(location, matchpattern, replacementpattern) + directory + imageName;
return rval;
If the source contains a trailing slash such as:
http://spdev/personal/administrator/_wpresources/SiteViewV4/1.0.0.0__9f4da00116c38ec5/
Please show what you want as the result text.
http://spdev/personal/administrator/_wpresources/SiteViewV4/1.0.0.0__9f4da00116c38ec5/
Please show what you want as the result text.
ASKER
http://spdev/personal/administrator/_wpresources/SiteViewV4/ from regexp and
http://spdev/personal/administrator/_wpresources/SiteViewV4/images/image1.jpg from the function.
http://spdev/personal/administrator/_wpresources/SiteViewV4/images/image1.jpg from the function.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.