RegEx验证字符串具有每个字母TRBL的0或1,没有重复,任何顺序 [英] RegEx to validate string has 0 or 1 of each letter TRBL, no dups, any order

查看:79
本文介绍了RegEx验证字符串具有每个字母TRBL的0或1,没有重复,任何顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证输入字符串,该输入字符串可以为空,也可以包含以任何顺序从"TRBL"中选择的最多四个字符.一个角色最多只能出现一次.

通过:LBRT TBRL B LT TL空"

失败:LLT(重复字母)
ZT(Z不在允许列表中)
tbrl(不允许小写)
"B T"(不允许使用空格)

希望得到任何建议-Steve

I need to validate an input string that can either be empty or contain up to four characters chosen from "TRBL" in any order. An individual character can appear no more than one time.

PASS: LBRT TBRL B LT TL "empty"

FAIL: LLT (duplicated letter)
ZT (Z not in allowed list)
tbrl (lower case not allowed)
"B T" (white space not allowed)

Would appreciate any suggestions - Steve

推荐答案

如果您从未听说过,这是一个很好的网站.包含备忘单和测试仪.以及预定义表达式的列表.

regexlib [ ^ ]

这是一个建议,我将看看是否可以提出解决方案!

看到您也将您的问题发布在该网站上:doh:无论如何,我会把这个问题留在这里,以供其他未曾听说过的人使用. :)
In case you have never heard of it, this is an excellent site. Contains cheat sheet and testers. As well as a list of predefined expressions.

regexlib[^]

That is a suggestion, i will see if i can come up with a solution!

See you posted your question on that site also :doh: I will leave this here anyway, for others who have maybe not heard of it. :)


我不会使用正则表达式:可以,但是会非常复杂且难以维护.
相反,我将对输入字符串中的字符进行排序,然后进行检查.

如果两个相邻字符相同,则失败.
如果集合"B","L","R","T"中没有任何字符,则失败
否则,通过.
I wouldn''t use a regex: you could but it would be quite complex and difficult to maintain.
Instead, I would sort the characters in my input string, and then check it.

If two adjacent characters are the same, fail.
If any character is not in the set ''B'', ''L'', ''R'', ''T'', fail
Otherwise, pass.


好吧,

基于此; http://regexlib.com/REDetails.aspx?regexp_id=564 [
Well,

Based on this; http://regexlib.com/REDetails.aspx?regexp_id=564[^]

I substiuted ABCD for TBRL and got this;

(?i:([TBRL])(?!\1)([TBRL])(?!\1|\2)([TBRL])(?!\1|\2|\3)([TBRL])) Didn''t work length had to be 4
Try (?i:([TBRL])?(?!\1)([TBRL])?(?!\1|\2)([TBRL])?(?!\1|\2|\3)([TBRL])?)

The tester results appears to work with an empty string also.

Worth a shot, i would be inclined to come up with my own algo for this though rather than use Regex.


这篇关于RegEx验证字符串具有每个字母TRBL的0或1,没有重复,任何顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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