1

I try to write a regex to validate a pattern, but I have some problems. This the pattern that I should get and validate using a regex

XXXX
XXXX-XXXX
XXXX-XXXX-XXXX-XXXX-XXXX

Every X is an Int number [0-9]

I'm trying with this :

[0-9]{4}(-?[0-9]{4})?

but it gives me this pattern :

**4586-4584**-**5588-5558**-**5888-5545**

enter image description here

Any idea how to fix this problem? Thanks in advance!

6
  • You can use ^[0-9{4}(?:-[0-9{4})*$ Commented Jan 18, 2018 at 10:15
  • Do you want to match 1, 2 or 5 appearances of XXXX (with hyphens, of course)? Commented Jan 18, 2018 at 10:21
  • @GalAbra I want to valide values like 5125 or 5125-3658 or 5885-9687-2545 the number of appearences of XXXX is 1 to 6 Commented Jan 18, 2018 at 10:23
  • @ErrabiAyoub With every number of groups? Commented Jan 18, 2018 at 10:23
  • @GalAbra the max character of this pattern should not be more than 35 "-" included Commented Jan 18, 2018 at 10:26

5 Answers 5

1

Your pattern [0-9]{4}(-?[0-9]{4})? uses an optional hyphen -? and an optional group (-?[0-9]{4}).

The optional hyphen -? will allow for example 8 digits in a row (4 digits followed by 4 digits).

The optional group will allow only 0 or 1 occurances so XXXX-XXXX-XXXX will not match.

Not using anchors for the beginning and the end of the line, will match 3333-3333 in 333-3333-3333.

You could update your regex to ^[0-9]{4}(-[0-9]{4})*$, or with \d: ^\d{4}(?:-\d{4})*$

Explanation

  • From the beginning of the string ^
  • 4 digits \d{4}
  • A non capturing group to match a hyphen and 4 digits (?:-\d{4})
  • Repeat that group zero or more times *
  • The end of the string $
Sign up to request clarification or add additional context in comments.

Comments

1
[0-9]{4}(-[0-9]{4})*

try this one, you need -, can't add ? ahead it. add * instead of ?, because may have more than one

(?<![0-9\-])[0-9]{4}(-[0-9]{4})*(?![0-9\-])

this one will not match 1234-5667-78961234

1 Comment

This regex allows values likes 5858-8589-55858888
1
^(\d{4})(-(\d{4})){0,5}$

If I understood correctly, you're looking for a pattern of XXXX (so every X is a digit) that appears 1 to 6 times, with hyphens in between.

Hence, the first 4 digits are mandatory, and then it's possible to have 1 to 5 appearances of -XXXX.

Note the ^ and the $, that make sure the regex doesn't match only a part of a string.

Comments

0

Try with this pattern:

    (\b([0-9]){4}\b)|(\b([0-9]){4}\-([0-9]){4}\b)|(\b([0-9]){4}\-([0-9]){4}\-([0-9]){4}\b)|(\b([0-9]){4}\-([0-9]){4}\-([0-9]){4}\-([0-9]){4}\b)

Demo

2 Comments

Thanks I try it it not working this regex allow values like 4588-5585-55546928
Upated. Maybe a shorted version but that one may work.
-2

This should do it. Only tried it using C# though.

[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}

1 Comment

This does not match 1234 or 1234-5678

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.