Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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!

Edit: 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. :)
 
Share this answer
 
v2
Comments
Espen Harlinn 26-Feb-11 13:05pm    
Good link, my 5
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.
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
Espen Harlinn 26-Feb-11 13:37pm    
Good advice - fooling around with c# beats fooling around with modbus, profibus, and all those other fantastic protocols you enjoy at work, doesn't it?
OriginalGriff 27-Feb-11 6:35am    
My only comments on that are that it returns a match "T" if you feed it "TTRL", or "TxRL", and it explicitly turns on "Ignore case" when the OP wanted lower case forbidden. You need "?-i:" instead.
DaveAuld 27-Feb-11 8:59am    
That is why i would custom algo it rather than use RegEx! to many hidden mysteries to regex mastery!
The first part of your question is not difficult, something like "[RBLT]{4}" would do it.

The 'no duplicates' is difficult and in my opinion not suitable for a regular expression. This[^] article agrees.

Although there are some regex wizards on CP that might give different advice.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Feb-11 14:29pm    
You're right, my 5. Regex along will not do the trick, but simple combination of methods will, see my variant.
--SA
It should be three steps: first, compare string with string.Empty (or first with null, or use string.IsNullOrEmpty). As a second step, use [RBLT]{4}, as Henry correctly put it. Third: with Regex, find all matches and then compare them in a loop to find duplicates and select only one match in each case. For uniqueness, you may want to use a dictionary of strings.

—SA
 
Share this answer
 
Comments
Henry Minute 26-Feb-11 14:57pm    
The dictionary makes sense.
Sergey Alexandrovich Kryukov 26-Feb-11 22:00pm    
Thank you, Henry.
--SA
This solution came from MRAB posting at <a href="http://regexadvice.com/forums/78568/ShowThread.aspx#78568">http://regexadvice.com/forums/78568/ShowThread.aspx#78568</a>[<a href="http://regexadvice.com/forums/78568/ShowThread.aspx#78568" target="_blank" title="New Window">^</a>]

Simple, yet elegant! Thanks - Gawiz

---------------------------------------------------------------

Try this: ^(?!.*(.).*\1)[TRBL]{0,4}$

This part:

.*(.).*\1

which will match if there's a repeated character in the string, and because it's in a negative lookahead a match will cause the regex as a whole to fail to match.

The remainder of the regex checks that the string consists of at most 4 of the letters "T", "R", "B", "L".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900