0

I want to check if my students get the RegEx to that Question right:

We want to have a number from 1 to 6 and afterwords a letter from a to d which could be written small or big. So things like 1d, 2A, 4b, ...

The RegEx I want to have from my students should be something like [1-6][a-dA-D] or [123456][abcdABCD] or mixtures of these two and all permutations of the second ones.

In my researches I found this answer for the permutations:

^(?:([1-4])(?!.*\1)){4}$

and it works by it's own (as well without the ^$) but when I combine it with a choices operator (|) it allows as well repetitions like [111111][a-dA-D] and that's not what I want to have.

That's what I try out until yet:

[\[]((?:([1-6])(?!.*\1)){6}|[1]\-[6])[\]][\[]((?:([a-dA-D])(?!.*\1)){8}|([a][\-][d][A][\-][D]|[A][\-][D][a][\-][d]))[\]]

Testdata:

[1-6][A-Da-d]
[123456][a-dA-D] 
[654321][AaBbCcDd]  
[241365][abcdABCD]
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
Pia
  • 1
  • 2
  • Can you make your question more expressive, add some sample examples and test data against which you want to run Regex? – Pramod Shinde Apr 23 '15 at 11:49
  • 3
    This seems like a bad way of validating their answers. It will be easier and clearer to just validate the answer against a set of test cases. – beerbajay Apr 23 '15 at 11:53
  • Either validate it against a set of test cases, or parse the regex pattern yourself to validate it - the assignment is simple so this kind of validation should be straightforward. Checking permutations isn't really a good fit for regex. – Lucas Trzesniewski Apr 23 '15 at 12:54
  • There is, except of my ideas, no test data jet. the problem is, that it is a moodle online class, which I construct for a course at my university (for using later in real life). In that formula it's only possible to enter 4 answers which are all clear text or RegEx. One I need to check all the wrong cases I have no other Idea than a RegEx for that one, so there are only three cases where I can check the right answers. I first didn't thought about the permutations but I'm afraid that my Prof checks these cases and than I'm in trouble... :/ – Pia Apr 23 '15 at 17:29
  • Depends also on the flavor of regex...which parser is being used? – ΩmegaMan Apr 23 '15 at 17:51
  • Thanks for all your help. I decided to tell them, that they have to write the answer in the correct alphabetic and numeric order and the small letters first.Like that the problem is gone. :) – Pia Apr 25 '15 at 18:31

1 Answers1

0

In order to validate regex I will suggest to run some unit tests to them validating what you are expecting to... at the minimum I'll have:

  • A test that validates that it is a valid Regular Expression. Compiling it.
  • A test that validates it matches a simple use case. 1A, 1B
  • Tests that validates against invalid use cases like A1, B1, ^d, 88D
Garis M Suero
  • 7,974
  • 7
  • 45
  • 68