By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Regex: select all possible of a pattern

dlite922
Expert 100+
P: 1,584
I'm putting this question in perl forum be we don't have a regex forum (we should) and 2, you guys rock at Regex.

I'm trying to find all patterns of a number to accommodate a dyslexic typer. By matching any pattern that contains the characters in the given input as many times as they /exist/ in the input. English sucks, example's better:

given 231

match 2 3 and 1 in any order, but do not repeat because there's only a single 1, a single 2, and a single 3:

123
132
213
231
312
321

but not:
111
112
221
222
311
333
223
332
etc...

My brain is locked. I'm thinking it should be easy but all i can come up with is along the lines of:
Expand|Select|Wrap|Line Numbers
  1.  (3|2|1)+ 
which of course matches ALL of the above unwanted outputs.


Thanks,



Dan
Aug 12 '09 #1
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
You can try this:
Expand|Select|Wrap|Line Numbers
  1. /^(\d)(?!\1)(\d)(?!\1)(?!\2)\d$/
  2.  
The negative lookahead assertion(?!) checks that the character that follows does not match the pattern mentioned inside the cluster.
Aug 13 '09 #2

dlite922
Expert 100+
P: 1,584
Thanks, ! suck at neg lookaheads.

I did change the \d to [123] though, because my character set must be those. Otherwise 456 would get through also.

Expand|Select|Wrap|Line Numbers
  1. ^([123])(?!\1)([123])(?!\1)(?!\2)[123]$

Dan
Aug 13 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.