?
Solved

How do I write a regular expression to trim a complex path name

Posted on 2008-06-12
7
Medium Priority
?
814 Views
Last Modified: 2008-06-13
I have a string with a path name eg. 'c:\microsoft\windows\shared'.  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\shared";
          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__9f4da00116c38ec5
I need to trim the final folder of the path using a reg exp.
0
Comment
Question by:millerthegorilla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 21771202
Please show what result you want and what result you get from the provided code.  Thanks.
0
 

Author Comment

by:millerthegorilla
ID: 21771362
I want back 'c:\....\siteview'

the provide code just returns the same string.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 21771473
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

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:millerthegorilla
ID: 21771747
here you go.  the variable location contains:
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;
        

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 21771845
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.
0
 
LVL 9

Accepted Solution

by:
ghostdog74 earned 2000 total points
ID: 21774679
you don't need a regular expression. Sometimes its best to do things simple. You can split on slashes "\", then join them back, excluding the last field.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month8 days, 1 hour left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question