1

I am trying to write a regex that matches my string with below format:

  • Total there will be 10 to 12 characters in a string.
  • And first 3 characters are distinct uppercase letters.
  • Then the next 4 characters represent the birth year and will always be between 1900 and 2019.
  • The next characters represent the number which can be either of these {10,20,50,100,200,500,1000}.
  • The last character is an uppercase letter.

I came up with below regex but having issues coming up for my third point. This line (1[9][0-9]{2}|2019) looks wrong and it doesn't work for string UYT20121000X:

^(([A-Z])(?!\2)([A-Z])(?!\2|\3)[A-Z])(1[9][0-9]{2}|2019)(10{1,3}|[25]00?)([A-Z])$

After using above regex I will extract the number which matches my fourth point.

1
  • Try it like this ^(([A-Z])(?!\2)([A-Z])(?!\2|\3)[A-Z])(?:19\d{2}|20[01]\d)(?:1000|[125]0?0)[A-Z]$ regex101.com/r/uHCjPD/1 Commented Oct 25, 2020 at 20:17

3 Answers 3

1

You could use (?:19\d{2}|20[01]\d) to match the range from 1900-2019.

It matches from 1900 till 1999 or from 2000 till 2019

The updated pattern could look like

^(([A-Z])(?!\2)([A-Z])(?!\2|\3)[A-Z])(19\d{2}|20[01]\d)(10{1,3}|[25]00?)([A-Z])$

Regex demo

Sign up to request clarification or add additional context in comments.

1 Comment

@user1950349 Ok then you can keep the capturing groups. I have put them back.
1

Try ^(([A-Z])(?!\2)([A-Z])(?!\2|\3)[A-Z])(19[0-9]{2}|20[01][0-9])(10{1,3}|[25]00?)([A-Z])$

The format you want is either 19aa or 20ba

where a is any digit and b is either 0 or 1

Comments

1

Your regex only matches exactly 2019 from this century, not the range 2000-2019.

To match the range 1900-2019:

(19\d\d|20[01]\d)

Putting in your regex (and removing unnecessary groups):

^([A-Z])(?!\1)([A-Z])(?!\1|\2)[A-Z](19\d\d|20[01]\d)(10{1,3}|[25]00?)([A-Z])$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.