Connecting Tech Pros Worldwide Help | Site Map

Regex: select all possible of a pattern

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#1: Aug 13 '09
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
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: Aug 13 '09

re: Regex: select all possible of a pattern


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.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,094
#3: Aug 13 '09

re: Regex: select all possible of a pattern


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
Reply


Similar Perl bytes