REGGEX Match only once in multiple delimiters

In an XYZ pattern where the delimiters are "-", I want to check if Y is size 8 without repeating.

Y can be a subset like Y = (ABC) but Y is 1 if not

1 - num-12345678-num -> In this case, I want the Y value to have a value. 2 - num-12345678-234-213-num -> Since Y is a subset of (12345678-234-213), Y must have a different meaning.

The regix I am using is '- ([0-9] *) -' and works for the 1st case, however it gets the same value for the second. Can anyone help me?

Thank you in advance

+3


source to share


1 answer


You can add a hyphen to the character class:

-([0-9-]*)-
      ^

      

See regex demo



If you put it at the end of the char class, you don't need to escape it.

More details

  • -

    - hyphen
  • ([0-9-]*)

    - group 1 capturing zero or more (due to quantifiers *

    ) digits and / and hyphens
  • -

    - literal hyphen again.
+1


source







All Articles