Multiple filters in a regex

0

I need to validate an input field that can have the following formats:

'D-1' ou 'D-10' ou 'D-1_1' ou 'D-10_1' ou 'D-1A' ou 'D-10A'

Letters and numbers may vary, but will always have one of these formats.

    
asked by anonymous 01.12.2017 / 01:22

1 answer

0

Use this regex:

([a-zA-Z]+-\d+_{0,1}[a-zA-Z0-9]*)

Explanation:

  • [a-zA-Z]+ Valid if one or more than one letter exists.
  • - Validates whether the - character exists after the letter.
  • \d+ Valid if one or more number exists.
  • _{0,1} Checks if after the previous sequence there exists the character _ if it exists it enters the capture group.
  • [a-zA-Z0-9]* Check if there are any letters or numbers after _ , if these characters are entered in the capture group.

You can see the operation of this regex here.

    
01.12.2017 / 12:43