What is the RegEx Pattern for filtering File Extensions?
-
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
-
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
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
-
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
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.
-
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
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
-
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.
sorry, the braces got lost... pattern= @"(?.+)\.(?[^.]+)$"; (i should use preview before posting...) /cadi 24 hours is not enough
-
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
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
-
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
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.
-
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
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
-
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
If you simply want to strip/change the File extension, look for the System.IO.Path[^] class. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
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.