Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Looking for a specific regex

Looking for a specific regex

Scheduled Pinned Locked Moved C#
questioncsharpregexhelp
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dennis Bork
    wrote on last edited by
    #1

    Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:

    Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt

    using this regex:

    (?<=_)[a-zA-z0-9]*(?=.txt$)

    I get:

    of_ABC123

    Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis

    P S L T P 5 Replies Last reply
    0
    • D Dennis Bork

      Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:

      Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt

      using this regex:

      (?<=_)[a-zA-z0-9]*(?=.txt$)

      I get:

      of_ABC123

      Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      First of all, if you have a regex question, there is a regular expression forum that should be used. Secondly, not every string manipulation requires a regex - a much simpler way would be to use string.LastIndexOf for the _ and IndexOf the . character and then just extract the substring from it.

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      1 Reply Last reply
      0
      • D Dennis Bork

        Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:

        Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt

        using this regex:

        (?<=_)[a-zA-z0-9]*(?=.txt$)

        I get:

        of_ABC123

        Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis

        S Offline
        S Offline
        savbace
        wrote on last edited by
        #3

        Here is an example of solution using both Regex and string class methods. In Regex I use a group with "FileCode" name to get result. As Pete said not all cases require using of Regex. And in this case I think would be better to use string methods (alternative variant).

                const string pattern = @"\_(?(\[^\_\]\[a-zA-Z0-9\])+)\\.txt$";
                const string text = @"Directory\\Subdirectory\\Subdir\\Subsubdir\\sub\_sub\_subv009\\filename\_with\_lots\_of\_ABC123.txt";
        
                var groupFileCode = Regex.Match(text, pattern);
        
                Console.WriteLine(groupFileCode.Groups\["FileCode"\].Value);
        
                // alternatively without regullar expressions
                int lastUnderscoreIndex = text.LastIndexOf('\_');
                int lastPointIndex = text.LastIndexOf('.');
        
                Console.WriteLine(text.Substring(lastUnderscoreIndex + 1, lastPointIndex - lastUnderscoreIndex - 1));
        

        Regards, savbace

        1 Reply Last reply
        0
        • D Dennis Bork

          Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:

          Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt

          using this regex:

          (?<=_)[a-zA-z0-9]*(?=.txt$)

          I get:

          of_ABC123

          Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis

          L Offline
          L Offline
          lmoelleb
          wrote on last edited by
          #4

          Your range is [a-zA-z0-9] - notice the second "z" is lower case, and as "_" falls between uppercase A and lowercase z it is included in your match.

          D 1 Reply Last reply
          0
          • L lmoelleb

            Your range is [a-zA-z0-9] - notice the second "z" is lower case, and as "_" falls between uppercase A and lowercase z it is included in your match.

            D Offline
            D Offline
            Dennis Bork
            wrote on last edited by
            #5

            Thank you so much! I have overseen this. So I have at last understood a little bit of regex and did it right. Thank you also for the other answers, especially the C# code examples. I will try them tomorrow!

            1 Reply Last reply
            0
            • D Dennis Bork

              Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:

              Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt

              using this regex:

              (?<=_)[a-zA-z0-9]*(?=.txt$)

              I get:

              of_ABC123

              Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis

              T Offline
              T Offline
              thewazz
              wrote on last edited by
              #6

              for reference, my regex links: learn: http://www.guistuff.com/articles/regexp\_full\_1.html http://net.tutsplus.com/tutorials/javascript-ajax/you-dont-know-anything-about-regular-expressions/ online testing: http://refiddle.com/ http://www.gethifi.com/tools/regex http://gskinner.com/RegExr/

              1 Reply Last reply
              0
              • D Dennis Bork

                Hey all, I am a bit disappointed by myself to come up with that question, but hey! there are some regex cracks here that can easily answer this question and maybe deepen my understanding. I really tried to figure out the best regex for my problem - but I am new to programming and regex' still seem to make my head explode. Here's the string I've got:

                Directory\Subdirectory\Subdir\Subsubdir\sub_sub_subv009\filename_with_lots_of_ABC123.txt

                using this regex:

                (?<=_)[a-zA-z0-9]*(?=.txt$)

                I get:

                of_ABC123

                Close call, but what I need is just ABC123 - this are all characters as a string between the LAST '_' and ".txt" Additionally, I did this in Expresso (did not use the designer but did a lot of test runs to see if my regex matches) - will this be the regex to be used in C#? I really appreciate any hint to solve my last problem, Best Dennis

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                I'd likely use System.IO.Path.GetFileNameWithoutExtension to narrow it down first, then use LastIndexOf and Substring as others have suggested. Were I to use a Regex, I'd likely use _(?'Name'[^_\.]+)\. I prefer to use named groups to capture what I want. You may want to look at the RightToLeft option as well; very handy for getting stuff that follows other stuff. Oh, and I use this handy tool: RegexTester[^] :-D

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups