views:

251

answers:

6

I need a regular expression to basically get the first part of a string, before the first slash ().

For example in the following:

C:\MyFolder\MyFile.zip

The part I need is "C:"

Another example:

somebucketname\MyFolder\MyFile.zip

I would need "somebucketname"

I also need a regular expression to retrieve the "right hand" part of it, so everything after the first slash (excluding the slash.)

For example

somebucketname\MyFolder\MyFile.zip

would return

MyFolder\MyFile.zip.

+2  A: 

You are aware that .NET's file handling classes do this a lot more elegantly, right?

For example in your last example, you could do:

FileInfo fi = new FileInfo(@"somebucketname\MyFolder\MyFile.zip");
string nameOnly = fi.Name;

The first example you could do:

FileInfo fi = new FileInfo(@"C:\MyFolder\MyFile.zip");
string driveOnly = fi.Root.Name.Replace(@"\", "");
Andy Shellam
While this works, doesn't it seem like overkill to create a FileInfo instance when simple substring gets the job done?
Andrew Hare
Agreed, I would only use RegEx where necessary. Built in classes handle these kinds of things with ease and avoid headaches in the future should problems arise from RegEx.
Pat
Yes but so does a regex ;-) It depends on what the code after this point needs to do - if it needs to act on one of the files somehow, then no it's not overkill.
Andy Shellam
yeah.. and it's not filesystem, it's amazon S3 i'm using - i concat. the bucketname to the file name to make a filesystem like path for the files...
alex
@alex - in that case the System.IO.FileInfo and System.IO.DirectoryInfo classes may work for you (they may not.)
Andy Shellam
+3  A: 

You don't need a regular expression (it would incur too much overhead for a simple problem like this), try this instead:

yourString = yourString.Substring(0, yourString.IndexOf('\\'));

And for finding everything after the first slash you can do this:

yourString = yourString.Substring(yourString.IndexOf('\\') + 1);
Andrew Hare
And also yourString.Substring(yourString.LastIndexOf('\')); for the second example.
Andy Shellam
@Andy - good point! :)
Andrew Hare
not quite... that would only return MyFile.zip - i need MyFolder\MyFile.zip
alex
@alex - I think Andy misunderstood your question - I edited my answer to provide the both solutions.
Andrew Hare
A: 

Here is the regular expression solution using the "greedy" operator '?'...

        var pattern = "^.*?\\\\";
        var m = Regex.Match("c:\\test\\gimmick.txt", pattern);
        MessageBox.Show(m.Captures[0].Value);
Software.Developer
A: 

Split on slash, then get first item

words = s.Split('\\');
words[0]
ghostdog74
+1  A: 

This matches all non \ chars

[^\\]*
+3  A: 

This problem can be handled quite cleanly with the .NET regular expression engine. What makes .NET regular expressions really nice is the ability to use named group captures.

Using a named group capture allows you to define a name for each part of regular expression you are interested in “capturing” that you can reference later to get at its value. The syntax for the group capture is "(?xxSome Regex Expressionxx). Remember also to include the System.Text.RegularExpressions import statement when using regular expression in your project.

Enjoy!

//Regular expression

  string _regex = @"(?<first_part>[a-zA-Z:0-9]+)\\{1}(?<second_part>(.)+)";

  //Example 1
  {
    Match match = Regex.Match(@"C:\MyFolder\MyFile.zip", _regex, RegexOptions.IgnoreCase);
    string firstPart = match.Groups["first_part"].Captures[0].Value;
    string secondPart = match.Groups["second_part"].Captures[0].Value;
  }

  //Example 2
  {
    Match match = Regex.Match(@"somebucketname\MyFolder\MyFile.zip", _regex, RegexOptions.IgnoreCase);
    string firstPart = match.Groups["first_part"].Captures[0].Value;
    string secondPart = match.Groups["second_part"].Captures[0].Value;
   }
Doug