• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • 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!

Reply to: Regular expression

Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "Regular expression"

Collapse

  • suityou01
    replied
    Originally posted by DimPrawn View Post
    PS. You should be using http://msdn.microsoft.com/en-us/libr...h_members.aspx to deal with paths, not regex.
    I know. Here's the rub. The tool was written by my predecessor who knows an awful lot about regexp.

    Choices are, rewrite of the tool, or ask for some help on this forum. Suprisingly this worked out quicker.

    This little nasty got dropped in my lap at 4:30.

    Leave a comment:


  • suityou01
    replied
    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.

    Top work DP

    Thanks

    Leave a comment:


  • swamp
    replied
    Forget what the regexp returns...

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

    use the first capture group (bit in brackets) from the above.

    Leave a comment:


  • DimPrawn
    replied
    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/libr...h_members.aspx to deal with paths, not regex.
    Last edited by DimPrawn; 24 September 2009, 17:14.

    Leave a comment:


  • suityou01
    replied
    Originally posted by suityou01 View Post
    In light of previous posts where I have got a right kicking from you, I just wanted to say thanks.
    .... for the help, not the kickins.

    Leave a comment:


  • suityou01
    replied
    Originally posted by DimPrawn View Post
    Something like

    Code:
    ^[a-zA-Z]{1}:\\(?<firstDir>[^\\]+)
    It looks great DP. I'm not having much luck though. It doesn't return anything.

    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



    In light of previous posts where I have got a right kicking from you, I just wanted to say thanks.

    Leave a comment:


  • DimPrawn
    replied
    Something like

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

    Leave a comment:


  • suityou01
    replied
    Originally posted by scotspine View Post
    this is the 2nd time you've had a regexp question here s/y [they're not my fav subject either!]

    it would be worth having a look at the regulator

    http://sourceforge.net/projects/regulator/

    as well as

    http://www.regexplib.com/

    these two should help a lot...
    Coincidentally its the second time I've been asked.

    Leave a comment:


  • scotspine
    replied
    this is the 2nd time you've had a regexp question here s/y [they're not my fav subject either!]

    it would be worth having a look at the regulator

    http://sourceforge.net/projects/regulator/

    as well as

    http://www.regexplib.com/

    these two should help a lot...

    Leave a comment:


  • suityou01
    started a topic Regular expression

    Regular expression

    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
Working...
X