-2

I have a complex product code to validate in Python code as below:

A8F30-ABCD-EFGH-IJKLM - for each letter I have around 7 different possibilities. Example:

1º (A) Character - Fix, all the time will be A;

2º (8) Character - Flexible, can be number 5 or 8 only;

3º (F) Character - Flexible, can be string F or M or G or C only;

4º (3) Character - Flexible, can be number 2 or 3 only;

5º (0) Character - Flexible, can be number 0 or 2 only;

6º (-) Character - FIX;

7º to 21º - Character - Flexible, can be string A or B or C or D or E or F or G;

How can I define the RegEx 'Regular Expression' to cover this validation code?

martineau
  • 99,260
  • 22
  • 139
  • 249
X15_Light
  • 27
  • 5
  • protip, you don't need a regex for this. Anyway, what did you try? where did you get stuck? – DeepSpace Jul 13 '20 at 18:51
  • 1
    Your example code does not match your description – Ron Rosenfeld Jul 13 '20 at 19:05
  • @WiktorStribiżew I see you've marked this as a duplicate of that reference again `Reference - What does this regex mean?` SI don't see a regex in the OP's question. To help us all out, can you explain what the duplicate is as it relates to this question ? –  Jul 13 '20 at 19:28
  • Since you have a bunch of `can be one of these` at the end, the common regex is `A[58][FMGC][23][02][-A-M]{16}` –  Jul 13 '20 at 19:42

2 Answers2

0

Here the solution :

(A)(5|8)(F|M|G|C)(2|3)(0|2)((-)(A|B|C|D|E|F|G){4,4}){3,3}
L. Quastana
  • 1,129
  • 11
  • 26
0

Try:

\bA[58][FMGC][23][02](?:-[ABCDEFG]{4}){3}\b

or, a bit shorter:

\bA[58][FMGC][23][02](?:-[A-G]{4}){3}\b

Since your example and description don't match, I assumed you really wanted three groups of -AAAA at the end where A could be in the set of [A-G].

But in your example, you show the last to be a group of five, and also letters that are not in [A-G].

The regex is easily modified when you clarify what you really want.

Ron Rosenfeld
  • 40,315
  • 6
  • 22
  • 49