-2

I need to write a regular expression to verify if a file input follows a rule:

A111B2_3_C.exe

in which:

  • A: compulsory, it must be a'A'
  • 111: compulsory, it is a natural number
  • B: compulsory, it must be a'B'
  • 2: compulsory, it is in range from 1->6
  • _: compulsory, it must be '_'
  • 3: compulsory, it must be a nature number
  • _: optional if C followed, it must be '_'
  • C: optional, C is a natural number
  • .exe: is the end extension of the file

It should be accepted this one: A111B2_3_.exe as well. but A111B2_3_0.exe is not accepted. the number after 3_ must be greater than 0.

Thanks in advance.

3
  • 1
    Um, why do you write C if it's supposed to be a number? Dec 30, 2011 at 15:26
  • 1
    nature number ? O.o . You mean Natural number ? Dec 30, 2011 at 17:11
  • Provide examples of what is valid and what is not besides the first one.
    – ΩmegaMan
    Jan 1, 2012 at 16:42

3 Answers 3

4

It's a bit hard to understand your requirements, but here's a regex that should do what you want:

A\d+B[1-6]_\d+(_\d+)?\.exe

Explanation:

  1. A -- The letter 'A'
  2. \d+ -- 1 or more numbers
  3. B -- The letter 'B'
  4. [1-6] -- A single number: 1, 2, 3, 4, 5, or 6
  5. _ -- An underscore
  6. \d+ -- 1 or more numbers
  7. (_\d+)? -- Optionally, an underscore followed by 1 or more numbers
  8. \. -- A period
  9. exe -- The letters 'exe'

Edit: In response to your comments, here is a regex that will also accept A111B2_3_.exe:

A\d+B[1-6]_\d+(_\d*)?\.exe

I changed the last \d+ (one or more numbers) to \d* (zero or more numbers).

Edit 2: Now I've changed \d* to ([1-9]\d*)?. What that means is left as an exercise to the reader.

A\d+B[1-6]_\d+(_([1-9]\d*)?)?\.exe
4
  • Hi, thanks a lot. But I would be also accept: A111B2_3_.exe as well. How can modify the current one for this?
    – olidev
    Jan 3, 2012 at 23:48
  • thanks. It is getting better. I forgot to mention, A111B2_3_0.exe is not accepted, the number after 3_ must be great than 0. thanks!
    – olidev
    Jan 4, 2012 at 0:05
  • I've edited my edit. At this point you should understand the regex well enough to edit it yourself if you need something else changed.
    – ean5533
    Jan 4, 2012 at 0:10
  • thanks. it works great. Yes, I have learnt some more knowledge from it. Thanks a lot!
    – olidev
    Jan 4, 2012 at 0:13
3

Try this:

var regex = new Regex(@"^A(\d+)B([1-6])_(\d+)(_(\d+))?\.exe$");
var match = regex.Match(filename);
if (match.Success) {
    var firstNumber = match.Groups[1].Value; // the '111' part (number)
    var secondNumber = match.Groups[2].Value; // the '2' part (1-6)
    var thirdNumber = match.Groups[3].Value; // the '3' part (number)
    var hasFourthNumber = match.Groups[4].Success;
    string fourthNumber = null;
    if (hasFourthNumber) {
        fourthNumber = match.Groups[5].Value; // the 'C' part (number)
    }

    // TODO: Do stuff with the extracted numbers
    // TODO: Give better variable names...
}

Check out a working version here: http://regexr.com?2vjto

5
  • @atomblad: while you may have presented working code, I think your answer would be better if you explained what the regular expression means rather than just giving the expression and showing what it returns. Otherwise, sequences like (\d+) will remain steeped in mystery. Dec 30, 2011 at 15:47
  • 1
    great. your provided sample works for me after changing something. but at least it is really the solution and also the link provided is very productive! thanks a lot!
    – olidev
    Jan 1, 2012 at 22:05
  • Hi, I tried your solution but I got this problem for: A111B2_3_.exe how should I modify it to accept this one? if there is a number between 3 and ".exe" then there must be " _ " in between: so: A111B2_3_.exe and A111B2_3_123.exe are both accepted but A111B2_3__123.exe is not accepted. Thanks in advance.
    – olidev
    Jan 3, 2012 at 23:50
  • Oh, that wasn't in your original specification of the problem. Should there always be that extra underscore character or is it optional? You originally wrote that the underscore was only required when followed by a number. Jan 4, 2012 at 7:09
  • For now you can change it to: ^A(\d+)B([1-6])_(\d+)(_)?(\d+)?\.exe$ and change the "hasFourthNumber" check to match.Groups[5].Success - sorry for bad formatting. Writing this on my phone. Jan 4, 2012 at 7:13
0

Something like this shuld work:

A\d\d\dB[1-6]_\d(_\d)?\.exe

This assumes that 111 is 3 digit, 2 is one digit and C in one digit. If 111,2 and C can have more digits:

A(\d+)B[1-6]_(\d+)(_(\d+))?\.exe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.