• Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Regular expression

suityou01

Potty-trained
Joined
Jan 20, 2009
Messages
21,122
Likes received
773
Thanks received
222
Location
Clown World
Hi,

I need some help with a regular expression. I am not a RegExp guy at all, and have no other way around the problem as the tool written uses regexp all over the place. If there was a way round I would take it.

That said.

How *** **** would you get the first directory out of the following string using regexp

C:\First directory\Second directory\Third directory\filename

^*\\*.*\\

returns me

'C:\First directory\'

Which is close but I just want

'First directory'

Any help greatly appreciated.

Thanks
 
Something like

Code:
^[a-zA-Z]{1}:\\(?<firstDir>[^\\]+)
 
Something like

Code:
^[a-zA-Z]{1}:\\(?<firstDir>[^\\]+)

It looks great DP. I'm not having much luck though. It doesn't return anything. :confused:

I tried breaking it down so I just used

^[a-zA-Z]{1}:

which in my limited grasp of regexp should return

C:

But it doesn't

^[a-zA-Z]{1}

returns

C

:suicide:

In light of previous posts where I have got a right kicking from you, I just wanted to say thanks. :hug:
 
Works for me using http://www.ultrapico.com/Expresso.htm

Code:
//  using System.Text.RegularExpressions;

/// <summary>
///  Regular expression built for C# on: Thu, Sep 24, 2009, 06:10:31 PM
///  Using Expresso Version: 3.0.3276, http://www.ultrapico.com
///  
///  A description of the regular expression:
///  
///  Beginning of line or string
///  Any character in this class: [a-zA-Z], exactly 1 repetitions
///  :\\
///      :
///      Literal \
///  [firstDir]: A named capture group. [[^\\]+]
///      Any character that is NOT in this class: [\\], one or more repetitions
///  
///
/// </summary>
public static Regex regex = new Regex(
      "^[a-zA-Z]{1}:\\\\(?<firstDir>[^\\\\]+)",
    RegexOptions.IgnoreCase
    | RegexOptions.Singleline
    | RegexOptions.ExplicitCapture
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );



//// Replace the matched text in the InputText using the replacement pattern
// string result = regex.Replace(InputText,regexReplace);

//// Split the InputText wherever the regex matches
// string[] results = regex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = regex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = regex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = regex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = regex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = regex.GetGroupNumbers();

The named group firstDir contains the complete name of the first directory.

PS. You should be using http://msdn.microsoft.com/en-us/library/system.io.path_members.aspx to deal with paths, not regex.
 
Last edited: 18:12
Forget what the regexp returns...

^[^\\]+\\([^\\]+)\\

use the first capture group (bit in brackets) from the above.
 
I know, and that's the kind of day I'm having.

If I take the "^" of the front it works superbly and now I don't need to stay here until 10pm. :D

Top work DP :yay:

Thanks
 

Advertisers

Back
Top