0

I need to validate file name with following pattern....

  1. File name(string) should not be null or empty
  2. File name(string) should have extension as .INI
  3. File name(string) should have "gen1" or "gen2" or "gen3"... genN where N has to be number.

We have already done the implementation with Javascript string functions but it looked little messy..

I am really interested in Validation-3 of "genN" which can be done more gracefully with RegEx

1
  • minitech's answer below raises a good point. Can you clarify what you mean by "should have 'gen1'"? That is -- can you give some examples of valid filenames? Commented Mar 23, 2012 at 15:22

2 Answers 2

2

By "should have genN", do you mean:

  • Should be named gen#.ini?

    /^gen\d\.ini$/i
    
  • Should contain gen#?

    /^.*gen\d.*\.ini$/i
    

Also, if you want more than 0 to 9 in those, change \d to \d+. If you only want to accept 1 and onwards, [1-9]. Both these requirements? [1-9]\d*.


Here's a helpful picker that should make the right regular expression for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Minitech... worked perfectly bud... You should have opt for maxtech avatar... :)
0

I agree, this sounds perfectly suited to regexes. Assuming that N has to be a positive decimal integer, and it can't have leading 0s, you can write:

/^gen[1-9][0-9]*\.INI$/

which means "start-of-string (^), followed by gen, followed by a digit in the range 1-9, followed by zero or more (*) digits in the range 0-9, followed by a literal dot (\.), followed by INI, followed by end-of-string ($)".

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.