0

I want to create a regex that does the following

<\numberMAX8>[space or not]<\symbol(-)>[space or not]<\numberMAX8> and max 10 times of all of this - I don't care about end spaces, also numbers must be between 5-8.

To explain it a bit more I'll give a few examples

ex:

5-6 7-6 8-8 6-7 ok

4-7 not ok //because of 4

7 - 6 ok

7-6-6-6 not ok because of the - in the middle

Below is what I have so far without having included the mid spaces.

^([5-8](?:-|\s)[5-8][\s]){1,10}
          ->  <-//didnt work.
11
  • 1
    Can you add all possible input formats with their validity Commented Mar 30, 2017 at 12:32
  • 1
    Maybe ^[5-8] ?- ?[5-8](?: [5-8] ?- ?[5-8]){0,9} *$ Commented Mar 30, 2017 at 12:35
  • 1
    Try using this: ^(?:[5-8]{0,8}\s*-\s*[5-8]{0,8}\s*){0,10}$ Commented Mar 30, 2017 at 12:35
  • 2
    What is not clear is how these values are separated one from the other. Whitespace? Try ^([5-8]\s*-\s*[5-8]\s*){1,10}$. Commented Mar 30, 2017 at 12:36
  • 1
    Try ^([5-8]\s*-\s*[5-8]\s*){1,10}$ Commented Mar 30, 2017 at 12:36

1 Answer 1

2

Here you go:

^([5-8]\s*-\s*[5-8]\s*){1,10}$

So the explanation is:

The regex matches a starting number from 5-8 ^[5-8], then an arbitrary number of spaces \s*, then dash -, then arbitrary number of spaces \s*, then a number from 5 to 8 [5-8], then an arbitrary number of spaces \s*, and that pattern from 1 to 10 times {1,10}, and nothing after the pattern $.

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

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.