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. What is the RegEx Pattern for filtering File Extensions?

What is the RegEx Pattern for filtering File Extensions?

Scheduled Pinned Locked Moved C#
regexquestion
10 Posts 5 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.
  • R Offline
    R Offline
    redfish34
    wrote on last edited by
    #1

    I have tried the following with no luck. I do not understand why NET RegEx does not handle this RegEx pattern. The pattern checks out fine in Regulator. string testString = @"c:\temp\test.wav"; // get file extension string pattern; pattern = @".+\.[^.]+$"; System.Text.RegularExpressions.Regex regEx; regEx = new System.Text.RegularExpressions.Regex(pattern); System.Text.RegularExpressions.MatchCollection matchList; matchList = regEx.Matches(testString); string foundString; foundString = matchList[0].Value; // the output will be the same as testString: c:\temp\test.wav. // the desired result is 'wav' Console.WriteLine(foundString); Happy Holidays -- modified at 8:48 Friday 23rd December, 2005

    C K S 3 Replies Last reply
    0
    • R redfish34

      I have tried the following with no luck. I do not understand why NET RegEx does not handle this RegEx pattern. The pattern checks out fine in Regulator. string testString = @"c:\temp\test.wav"; // get file extension string pattern; pattern = @".+\.[^.]+$"; System.Text.RegularExpressions.Regex regEx; regEx = new System.Text.RegularExpressions.Regex(pattern); System.Text.RegularExpressions.MatchCollection matchList; matchList = regEx.Matches(testString); string foundString; foundString = matchList[0].Value; // the output will be the same as testString: c:\temp\test.wav. // the desired result is 'wav' Console.WriteLine(foundString); Happy Holidays -- modified at 8:48 Friday 23rd December, 2005

      C Offline
      C Offline
      Carsten Zeumer
      wrote on last edited by
      #2

      hi redfish34, try something like this: pattern= @"(?.+)\.(?[^.]+)$"; then you can access the extension by matchList[0].Groups["extension"].Value /cadi 24 hours is not enough

      R L 3 Replies Last reply
      0
      • C Carsten Zeumer

        hi redfish34, try something like this: pattern= @"(?.+)\.(?[^.]+)$"; then you can access the extension by matchList[0].Groups["extension"].Value /cadi 24 hours is not enough

        R Offline
        R Offline
        redfish34
        wrote on last edited by
        #3

        I got a "Unrecognized grouping construct" in both Regulator and VS 2003. But thanks for the effort! The RegEx i am using came direct from http://www.regexlib.com/ and checks out fine in Regulator, so i do not understand why the NET 1.1 RegEx is not liking it. Maybe this is a bug with NET and i will be forced to parse the extension by hand. It is no big deal, i just don't like all these Microsoft headaches.

        C 1 Reply Last reply
        0
        • C Carsten Zeumer

          hi redfish34, try something like this: pattern= @"(?.+)\.(?[^.]+)$"; then you can access the extension by matchList[0].Groups["extension"].Value /cadi 24 hours is not enough

          R Offline
          R Offline
          redfish34
          wrote on last edited by
          #4

          Your mentioning of groups got me thinking. I tried the following code with my RegEx pattern: foundString = matchList[0].Groups[1].ToString(); It returns the file extension "wav", which is what i wanted. I guess when one uses a capture expression in a RegEx then one must use groups to access the matches. Thanks

          C 1 Reply Last reply
          0
          • R redfish34

            I got a "Unrecognized grouping construct" in both Regulator and VS 2003. But thanks for the effort! The RegEx i am using came direct from http://www.regexlib.com/ and checks out fine in Regulator, so i do not understand why the NET 1.1 RegEx is not liking it. Maybe this is a bug with NET and i will be forced to parse the extension by hand. It is no big deal, i just don't like all these Microsoft headaches.

            C Offline
            C Offline
            Carsten Zeumer
            wrote on last edited by
            #5

            sorry, the braces got lost... pattern= @"(?.+)\.(?[^.]+)$"; (i should use preview before posting...) /cadi 24 hours is not enough

            1 Reply Last reply
            0
            • R redfish34

              Your mentioning of groups got me thinking. I tried the following code with my RegEx pattern: foundString = matchList[0].Groups[1].ToString(); It returns the file extension "wav", which is what i wanted. I guess when one uses a capture expression in a RegEx then one must use groups to access the matches. Thanks

              C Offline
              C Offline
              Carsten Zeumer
              wrote on last edited by
              #6

              there is one other solution using a non capturing group: (?<=.+\.)[^.]+$ this will have only the extension in the resulting match. /cadi 24 hours is not enough

              1 Reply Last reply
              0
              • R redfish34

                I have tried the following with no luck. I do not understand why NET RegEx does not handle this RegEx pattern. The pattern checks out fine in Regulator. string testString = @"c:\temp\test.wav"; // get file extension string pattern; pattern = @".+\.[^.]+$"; System.Text.RegularExpressions.Regex regEx; regEx = new System.Text.RegularExpressions.Regex(pattern); System.Text.RegularExpressions.MatchCollection matchList; matchList = regEx.Matches(testString); string foundString; foundString = matchList[0].Value; // the output will be the same as testString: c:\temp\test.wav. // the desired result is 'wav' Console.WriteLine(foundString); Happy Holidays -- modified at 8:48 Friday 23rd December, 2005

                K Offline
                K Offline
                KaptinKrunch
                wrote on last edited by
                #7

                you could skip regex and use FileInfo. FileInfo fi = new FileInfo(filename); string ext = fi.Extension(); Just an idea. Not sure what would be faster tho.

                R 1 Reply Last reply
                0
                • C Carsten Zeumer

                  hi redfish34, try something like this: pattern= @"(?.+)\.(?[^.]+)$"; then you can access the extension by matchList[0].Groups["extension"].Value /cadi 24 hours is not enough

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  cadi wrote:

                  [^.]+

                  :doh: I think it pretty clear to you now ;P (if not, read match one or more of (anything but anything)) xacc.ide-0.1.1 released! :) Download and screenshots

                  1 Reply Last reply
                  0
                  • R redfish34

                    I have tried the following with no luck. I do not understand why NET RegEx does not handle this RegEx pattern. The pattern checks out fine in Regulator. string testString = @"c:\temp\test.wav"; // get file extension string pattern; pattern = @".+\.[^.]+$"; System.Text.RegularExpressions.Regex regEx; regEx = new System.Text.RegularExpressions.Regex(pattern); System.Text.RegularExpressions.MatchCollection matchList; matchList = regEx.Matches(testString); string foundString; foundString = matchList[0].Value; // the output will be the same as testString: c:\temp\test.wav. // the desired result is 'wav' Console.WriteLine(foundString); Happy Holidays -- modified at 8:48 Friday 23rd December, 2005

                    S Offline
                    S Offline
                    S Senthil Kumar
                    wrote on last edited by
                    #9

                    If you simply want to strip/change the File extension, look for the System.IO.Path[^] class. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                    1 Reply Last reply
                    0
                    • K KaptinKrunch

                      you could skip regex and use FileInfo. FileInfo fi = new FileInfo(filename); string ext = fi.Extension(); Just an idea. Not sure what would be faster tho.

                      R Offline
                      R Offline
                      redfish34
                      wrote on last edited by
                      #10

                      I did not notice the "Extension" property of the FileInfo object. That is definitely a better solution. Well, at least i got some RegEx practice from all this. I will be using RegEx extensively in my next project.

                      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