0

I want to check that a String is pipe-generated numbers. There should be numbers between pipes.

  • Valid Strings examples: 300, 300|600.
  • Invalid Strings examples: 300||||600

I tried ^([\d|\d])*$. However, this still said that 300||||600 is a valid String.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
  • What about `300|600|900`? – dvo Oct 07 '19 at 18:52
  • `^([\d|\d])*$` matches a digit or `|` any number of times, in any order. I recommend going to regex101.com to see what your pattern is doing. https://regex101.com/r/rLV5IC/1 – CAustin Oct 07 '19 at 18:54

1 Answers1

1

Here, what you want is one number, ie \d+, followed by an undetermined number of occurrences of a pipe then a number, which would be (\|\d+)* (the pipe is escaped).

As you want it to cover the whole input this would be

^\d+(\|\d+)*$
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758