Several filters in a regex

Asked

Viewed 75 times

0

I need to validate a field input which may 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 they will always have one of these formats.

  • You want to check if the input will have only one of these formats or contains one of these formats?

  • 2

    https://regex101.com/r/ehRMdv/1?

1 answer

0


Use that regex:

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

Explanation:

  • [a-zA-Z]+ Validates if there is one or more of a letter.
  • - Validates if there is a character - after the letter.
  • \d+ Validates if there is one or more.
  • _{0,1} Checks if after the previous sequence there is the character _ if it exists it enters the capture group.
  • [a-zA-Z0-9]* Checks for letters or numbers after _, if such characters exist enter the capture group.

You can see how this regex works here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.