具有换行符的逗号分隔列表的正则表达式 [英] Regex for comma delimited list with line breaks

查看:118
本文介绍了具有换行符的逗号分隔列表的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的文本框中允许的格式是逗号分隔的列表,后面是逗号分隔列表之间的换行符。以下是使用者想要的例子:

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

到目前为止限制了使用此验证表达式的用户:

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

该表达式,用户只能输入一行逗号分隔的数字。



如何接受换行接受多行?

解决方案

可以检查输入的格式是否正确。我建议使用组并重复:

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

但是要检查矩阵是否是对称的,你必须使用其他的东西才能正则表达式。 >

请查看: https:// regex101。 com / r / GqtOuQ / 2 /



如果您想要更加用户友好,可以允许与用户想要的水平空间一样多在数字和逗号之间添加。这可以通过他正则表达式组 \h 完成,它允许除 \\\
之外的每个空格。



正则表达式现在看起来更混乱:

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

请在这里查看: https:// regex101.com/r/GqtOuQ/3



以下是适用于.NET的版本:



[pre> (([\t] * \d + [\t] *,[\t] *)+ [\t] * \d + [\\ \\ t] * \\\
?[\t] *)+


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

这篇关于具有换行符的逗号分隔列表的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆