Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: regular-expression
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 26-Feb-11 6:21am
Gawiz379
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 D'Oh! | :doh: I will leave this here anyway, for others who have maybe not heard of it. Smile | :)
  Permalink  
v2
Comments
Espen Harlinn at 26-Feb-11 13:05pm
   
Good link, my 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v2
Comments
Espen Harlinn at 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 at 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 at 27-Feb-11 8:59am
   
That is why i would custom algo it rather than use RegEx! to many hidden mysteries to regex mastery!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Comments
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Comments
Henry Minute at 26-Feb-11 14:57pm
   
The dictionary makes sense.
SAKryukov at 26-Feb-11 22:00pm
   
Thank you, Henry.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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".
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 384
3 PIEBALDconsult 235
4 BillWoodruff 190


Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 26 Feb 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100