Regex for comma delimited list with line breaks

The format I would like to allow in my text boxes are comma delimited lists followed by a line break between comma delimited lists. Here's an example of what I want from a user:

1,2,3
1,2,4
1,2,5
1,2,6

      

So far I have restricted the user using this ValidationExpression:

^([1-9][0-9]*[]*[ ]*,[ ]*)*[1-9][0-9]*$

      

However, with this expression, the user can only enter one line of comma-delimited numbers.

How can one accept multiple lines while accepting line breaks?

+3


source to share


1 answer


You can check if the input is in the correct format. I would recommend using groups and repeating them:

((\d+,)+\d+\n?)+

      

But to check if a matrix is ​​symmetric you need to use something else and then regex.

Take a look here: https://regex101.com/r/GqtOuQ/2/

If you want to be more user-friendly, you can allow as many horizontal spaces as the user wants to add between the number and the comma. This can be done with the regex group \h

, which allows all spaces except \n

.



The regex code looks a little messier:

((\h*\d+\h*,\h*)+\h*\d+\h*\n?\h*)+

      

Check it out here: https://regex101.com/r/GqtOuQ/3

Here is the version that should work with .NET:

(([ \t]*\d+[ \t]*,[ \t]*)+[ \t]*\d+[ \t]*\n?[ \t]*)+

      

+3


source







All Articles