• 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!

Regular expression

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    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
    Knock first as I might be balancing my chakras.

    #2
    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...

    Comment


      #3
      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.
      Knock first as I might be balancing my chakras.

      Comment


        #4
        Something like

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

        Comment


          #5
          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.
          Knock first as I might be balancing my chakras.

          Comment


            #6
            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.
            Knock first as I might be balancing my chakras.

            Comment


              #7
              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.

              Comment


                #8
                Forget what the regexp returns...

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

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

                Comment


                  #9
                  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
                  Knock first as I might be balancing my chakras.

                  Comment


                    #10
                    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.
                    Knock first as I might be balancing my chakras.

                    Comment

                    Working...
                    X