0

I want to code a regular expression to match alphanumeric strings with the following constraints:

  1. Contains at least one character (a-zA-Z)
  2. Does not contain the name of the months of the year
  3. Accepts any symbol or character
  4. length between: 8 and 90

  • Valid strings: "ABG-1234","ABG_3423", "ZIZ@342!2324^$^$^"(){}, "1982TEST", "09 87 34 56 aaa"
  • Invalid strings: "123456789", "1234", "JANVIER 89", "DEC 345", "333"

Possible solution:

     ^           # anchor at the start
    (?=.*\d)     # must contain at least one numeric character
    (?!.*(?:JANVIER|F[Eé]VRIER|MARS|AVRIL|MAI|JUIN|JUILLET|AO[Uù]T|SEPTEMBRE|OCTOBRE|NOVEMBRE|D[Eé]CEMBRE|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))#Does not contain any month name
   (\w)          # Any Character
    (?=.*[a-z])  # must contain one lowercase character
    (?=.*[A-Z])  # must contain one uppercase character
    .{8,90}      # From 8 to 10 characters in length
     $            # anchor at the end"

Hani Goc
  • 2,371
  • 5
  • 45
  • 89
  • 1
    Could you post a valid and an invalid string that you are testing against. – Peter R Feb 23 '14 at 11:50
  • 1
    Have you considered splitting this up into multiple requests, it seems your constraints are independent of each other. Also if you want more constraints later you don't need an even more complex regex. Just makes it simpler to read as a programmer. Do you have a reason to do it all in one call? – Elliott Hill Feb 23 '14 at 12:19
  • @ElliottHill The reason is that the following regular expression should not be included in the source code. I am loading it from a text file. Second i have other regular expressions that are used to match phone numbers, numeric strings etc. For me it seems better to do it directly i don't know. Can you check the possible solution that I posted? So far it's matching the strings correctly. – Hani Goc Feb 23 '14 at 12:24
  • It's possible you could have multiple regexes in one file and load them in one at a time. It looks correct to me, however it seems you have some good test cases, if you can expand them to cover all conditions and they all work then I'd trust that over my opinion any day. – Elliott Hill Feb 23 '14 at 12:41
  • 1
    Why not test `FÉVRIER` and `DÉCEMBRE` with an `É`. Also it is `AOÛT` not `AOÙT` – Toto Feb 23 '14 at 14:07
  • @M42 I can do. But this is not the issue. My problem is the following constraints that i posted. thank u – Hani Goc Feb 23 '14 at 14:11

1 Answers1

1

regex are fine when you want to extract some data. Here you juste want to check different characteristics. IMHO, you should proceed this way :

  1. check size (O(1))
  2. search for any alpha char (O(n))
  3. search for months (here a regex could be interesting)

Each feature can be checked by independant regexs. You could write all of them, then merge them into a single one that performs a logical AND when searching.

Take a look at the top answer of this topic : Combine Regexp

Community
  • 1
  • 1
Kiwi
  • 2,698
  • 16
  • 15