Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
How to make a regex discard all if not matched exactly?

I'm working on a regex that needs this format:

X = Integer

XXXX-XX

Where it only matches if follow these rules:

  1. Before "-" it need to be 4 integers.
  2. The "-" is optional, but if there is a 5th character it has to be "-"
  3. After "-" it can be 0, 1 or 2 integers
  4. The "-" only matches if there is integers after it.

Here is what i have been done for each rule respectively to work:

The regex: (\d{4})-?(\d{1,2})?((?<!(\d{5})|(\d{4}-)))

  1. (\d{4})
  2. -? and ((?<!(\d{5})|(\d{4}-))) (negative lookahead to avoid match "11111")
  3. (\d{1,2})?
  4. ((?<!(\d{5})|(\d{4}-)))

My problem is, i want to discard the whole string if anything is wrong.

For example

In: 123456 It matches 123456

In: 1234-5678 It matches 1234-5678

I would like to discard the whole string if it doesnt match the pattern, how can i do it?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!