SpcCode
SpcCode

Reputation: 917

Regex expression for a chosen file name format

I am want to implement some regex to test a valid filename as: ABCD-A-V1-02/27/2012

Where the first letter should not be more than 4 letters, the second group goes from A-to-Z and the V# like V1, V2, etc

Here is what I have until now, but in the reg tester does not work, I think I a missing something.

[a-zA-Z]{4}-[A-Z]{1}-V\d{0,9}[1-9])|(0[1-9])|(1[0-2]))\/(([0-9])|([0-2][0-9])|(3[0-1]))\/(([0-9][0-9])|([1-2][0,9][0-9][0-9]

Upvotes: 1

Views: 324

Answers (2)

Lance U. Matthews
Lance U. Matthews

Reputation: 16606

Here's the problems I see based on your description:

  • The quantifier for the first group of alphabetic characters is requiring exactly four characters instead of up to four characters
  • The pattern doesn't contain the second group of alphabetic character(s) ('A')
  • The pattern doesn't contain the forward slashes for the date.
  • You've got some things going on inside of curly brackets that aren't valid

Try this:

@"[a-zA-Z]{1,4}-[A-Z]-V\d-\d{2}/\d{2}/\d{4}"

That breaks down as:

  • At least one and no more than four alphabetic characters, case-insensitive
  • A hyphen
  • A single uppercase alphabetic character
  • A hyphen
  • An uppercase 'V'
  • A single digit
  • A hyphen
  • Two digits
  • A forward slash
  • Two digits
  • A forward slash
  • Four digits

Obviously this won't ensure that the digits at the end represent a valid date.

Upvotes: 2

Nikson Kanti Paul
Nikson Kanti Paul

Reputation: 3440

also you can put ^...$ sign for just validate your desire pattern exactly

 @"^[a-zA-Z]{1,4}-[A-Z]-V\d-\d{2}/\d{2}/\d{4}$"

it will just validate only your desire pattern , rest of all is invalid

valid : AAAa-aa-v232-12/12/2-2010
invlid: AAAa-aa-v232-12/12/2-20103343AAAA

Upvotes: 0

Related Questions