Expresión regular para validar números separados por delimitadores

I need a regular expression to validate the an input from a form with those values

por ejemplo:

123, 444,231, 2231 ...

what is the best way regular expression for that cycle?

preguntado el 02 de febrero de 12 a las 10:02

can you clarify your question? -

What format the values should have? -

4 Respuestas

If I understand your question correctly, you want to match on the numbers?

Utilizar esta:

([0-9]{1,}+){1,}

That will match each of the values.

Respondido 02 Feb 12, 14:02

But, personally, I wouldn't use a regex for this. C# has a native way to split based on string delimiters, in this case, a comma. - Barry Chapman

([1-9]\d{2,3},\s*)  //first one digit 1-9, followed by 2 to 3 other digits,a comma and zero or one whitespace
{0,}            //This pattern may occour 0 or more times, followed by...
([1-9]\d{2,3})  // one of these number without the trailing comma, only once.

Respondido 02 Feb 12, 15:02

@Tomek, that's because he didn't give any example where there were no commas between the numbers. ([1-9]\d{2,3}\s*,\s) will work. It won't work with multiple spaces neither, because that was not in the example given. Change accordingly. - Pengtuzi

I can suggest you the following. ^(\s*\d+\s*,?)+$ it takes into consideration the possiblity of leading and trailing spaces of your CSV values

Editar

I modified the expression and here is the new version: ^(\s*\d+\s*,)*(\s*\d+\s*){1}$ It wil not fail in commented case.

Respondido 02 Feb 12, 15:02

The specification of requirements is bit vague, but this might be what you want:

^(\s*\d+\s*(?(?=,\s*\d),|))+$

It validates the sequence of numbers delimited by "," allowing leading and trailing white characters and not starting and ending by ","

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.