用于检查多行文本框输入的正则表达式
假设我有一个多行文本框,我想检查最多 5 行中的 1-2 位数字。我在这里找到了另一个类似问题的正则表达式模式的答案,但即使修改了多次,它对我来说也不起作用。
我目前正在使用以下内容但没有成功。
Dim textCheck As New Regex("(^\d{1,2}$\r?\n?){0,5}", RegexOptions.Multiline)
有人可以帮我解决我做错的事情吗?
谢谢
Lets say I have a multiline textbox that I would like to have checked for say, 1-2 digits over a max of 5 lines. I found a regular expression pattern answered on another similar question on here but it was not working for me even after modifying it a number of times.
I'm currently using the following without success.
Dim textCheck As New Regex("(^\d{1,2}$\r?\n?){0,5}", RegexOptions.Multiline)
Could somebody help me out with what I am doing wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么您想要匹配由换行符分隔的 1 到 2 位数字的列表,最多 5 个?如果是这样,这应该可行。最后一个换行符是可选的,如果字符串中还有其他内容,则它不匹配。 (为此,不要使用 RegexOptions.Multiline)
我用 C# 检查了这一点,所以我不确定转义字符是否正确。我注意到你的 d 之前只有 1 个斜杠。在 C# 中,你需要两个,但我将其从中删除,使其看起来像你的。
So you're wanting to match a list of 1 to 2 digit numbers separated by a newline, up to five? if so, this should work. the last newline is optional and if theres anything else in the string it doesn't match. (for this, don't use RegexOptions.Multiline)
I checked this with C#, so I'm not sure if the escape characters are correct. i noticed yours only had 1 slash before the d. in c# you need two, but i removed it from this to make it look like yours.
首先,获取 RegEx Designer 的副本。它是免费的并且值得为这种事情付出代价。
http://www.radsoftware.com.au/?from=RegexDesigner
然后,我想你可能想要的是这样的
(^\d{1,2}\r?\n?){0,5}\z
然后测试匹配是否包含整个输入。中间的 $ 没有帮助, \z 强制匹配到字符串的末尾。不过,我可能错过了一些细节。 RegExDesigner 再次让使用正则表达式变得更加有趣!
First, Grab a copy of RegEx Designer. it's free and worth it's wieght for this kind of thing.
http://www.radsoftware.com.au/?from=RegexDesigner
Then, I think what you might want is something like this
(^\d{1,2}\r?\n?){0,5}\z
and then test that the match includes the entire input. The $ in the middle won't help, the \z forces the match to the end of string. There's probably some details I've missed though. Again, RegExDesigner makes playing with regexes sooooo much more enjoyable!