正则表达式:序列
被一个愚蠢的问题困住了。我有一个输入字段,用户可以在其中输入有限数量的字母(ABCDEFG)。问题是:我不希望用户在输入的单个子序列中能够有超过 3 个 A、C、E 和 G 的字母,即:没有 AAA、CCC、EEE、GGG。而第二件事几乎和第一件事一样:单个子序列中不超过1个B、D、F,即:没有BB、DD、FF。这两条规则以某种方式结合在一起。
因此,例如,AABFGECC 是有效的。 GEFFAABG 无效。
希望,你会帮助我!谢谢你!
PS 如果这很重要,我正在使用 Visual Basic 编写我的应用程序。但我认为,这并不是那么重要。
Stuck with a silly problem. I have an input field, where user can input limited amount of letters (ABCDEFG). Here is the problem: I do not want users to be able to have more than 3 A, C, E and G's letters in a single subsequence of the input, that is: no AAA, CCC, EEE, GGG. And the second thing is almost the same as the first one: no more than 1 B, D, F in a single subsequence, that is: no BB, DD, FF. These two rules are somehow to be combined together.
So, for example, AABFGECC is valid. GEFFFAABG is invalid.
Hope, you will help me! Thank you!
P.S If it is important, I am writing my app in Visual Basic. But I think, this is not so important.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您创建了一个与您想要避免的情况相匹配的表达式,而是检查输入是否不匹配,该怎么办?像这样:
What if you made an expression which matches the cases you want to avoid and instead check that the input does not match? Like this:
虽然您可以聪明地使用反向引用,但一个简单的解决方案是使用 否定前瞻:
从逻辑上讲,这与在列表中包含这 7 个无效序列,并检查字符串不包含其中任何一个序列相同,这也为您提供了一个不错的选择。
While you could be clever and use back-references, a simple solution is to black-list the invalid sequences using a negative look ahead:
Logically, that is the same has having those 7 invalid sequences in a list, and checking the string does not contain either one, which also gives you a nice alternative.