tags:

views:

80

answers:

2

Hi all, i've been messing around trying figure this out myself but its taking a while..

Basically after a regex to pass the following tests:

IsARarFile("test.rar"); // true
IsARarFile("test.r00"); // true
IsARarFile("test.txt"); // false
IsARarFile("test.avi"); // false
IsARarFile("test.mp4"); // false
IsARarFile("test.001"); // true
IsARarFile("test.ba00"); // false

Thanks for any help

+6  A: 
\.(?:rar|r\d\d|\d\d\d)$

I think.

Edit: Credit to Peter for another correction.

Reinderien
Why do you need to capture? use (?:...)
Anders
Since your edit it now seems to workPerfect, basically I just want to know which files in a directory are rar files. Seems pretty simple now that I look at it :)
BenW
Is the dot *not* a metacharacter in C#'s regex engine?
quantumSoup
Thanks for the corrections.
Reinderien
Just to understand then, the \. is saying that check for a '.' then either: rar or r digit digit or digit digit digit?
BenW
Jeffrey Friedl would probably formulate himself differently, but yes, you're correct :)
Anders
@BenW yep. Although the regex will match a file named '.000' (ie: no filename)
quantumSoup
This answer is wrong! Currently it will match "wr00ng.gif" and various other incorrect values, since the alternation is including the `\.` and `$` in first and last alternatives.It should only have one set of parens, surrounding the alternatives, like this: `\.(?:rar|r\d\d|\d\d\d)$` so that the `\.` and `$` are distinct from the extension choices.
Peter Boughton
+5  A: 
([^\.]+)\.(r(ar|\d\d)|\d{3})$ 
quantumSoup
+1 I like this one, however I feel the readability is better in Reinderiens answer, at least for people not familiar with regular expressions.
Anders
The first part is incorrect - both because the `.` doesn't need escaping inside a char class, and because filenames can have multiple `.` characters in them. (It might not be sensible to have a file called e.g. `.....rar`, but it's still valid.)
Peter Boughton