简体   繁体   中英

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 in between the comma delimited lists. Here is an example of what I want from the user:

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

So far I have limited the user using this ValidationExpression:

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

However with that expression, the user is only able to enter one row of comma delimited numbers.

How can proceed to accept multiple rows by accepting line breaks?

It is possible to check if the input has the correct format. I would recommend to use groups and repeat them:

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

But to check if the matrix is symmetric you have to use something else then regex.

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

If you want to be a bit more user friendly it is possible to allow as much horizontal spaces as the user wants to add between the number and comma. This can be done with he regex group \\h which allows every whitespace except \\n .

The regex code looks now a bit more messy:

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

Check this 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]*)+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM