Match a string that does not have a number after the letter

advertisements

I've got a scenario as follows. Our systems needs to pull filters from a string passed in as a query parameter, but also throw a 404 error if the string isn't correctly formatted. So let's take the following three strings as an exmple:

  1. pf0pt1000r
  2. pfasdfadf
  3. pf2000pt2100

By the application requirements, only #3 is supposed to match as a "valid" string. So my current regex to match that is /([a-z]+)(\d+)/. But this also matches #1, if not entirely, but it still matches.

My problem thus is twofold - I need 2 patterns, 1 that will match only the 3rd string in this list, and another that will match the "not-acceptable" strings 1 and 2. I believe there must be some way to "negate" a pattern (so then I'd technically only need one pattern, I'm assuming), but I'm not sure how exactly to do that.

Thanks for any help!

EDIT

For clarity's sake, let me explain. The "filter parameters" present here take the following structure - 1 or 2 letters, followed by a number of, well, numbers. That structure can repeat itself however many times. So for example, valid filter strings could be

  • pf100pt2000
  • pf100pt2000r2wp0to1
  • etc.

Invalid strings could be

  • pf10000pt2000r
  • pf3000pt2123wpno
  • ... anything not following the structure above.

You can use this regex for valid input:

^([a-zA-Z]+\d+)+$

RegEx Demo 1

To find invalid inputs use:

^(?!([a-zA-Z]+\d+)+$).+$

RegEx Demo 2