Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern Match all combinations 1

Status
Not open for further replies.

JJ1

Programmer
Apr 19, 2001
104
0
0
GB
Could anyone help me with this problem please:

I would like to match the letters "E", "N" and "L" in a string. All these letters must be present, but in any order.

Therefore, the following are possible:
[red]"ENL" or "ELN" or "LEN" or "ELN" or "NEL" or "NLE" etc...[/red]

I thought something like this maight work:
[red]if( $string =~ m/ELN/g ){
#Do something...
}[/red]
but it doesn't seem to.

Thanks in advance for any help.
Cheers,

James.
 
I think I may have solved it like this:
[red]
if ($string =~ m/^[E|N|L][E|N|L][E|N|L]$/) {
$content = "ENL";
}
[/red]

If anyone knows any tidier way, then please let me know!

All the best,

James.
 
I think the longer pattern, is more readable

if(/ENL|ELN|LEN|LNE|NLE|NEL/){
# it's in here somewhere!
}

Depends which way you think I suppose -- but spare a thought for thick people like me who might have to maintain your code afterwards. :) Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
James,

your regex will also match things like 'EEE', 'LLN', etc. which seem to fall outside your specifications.

btw, the pipe char, '|', is treated as a literal character in a character set (characters grouped together in []). it's not or'ing the letters together like you intend. What you probably meant to do was
Code:
 m/^[ENL][ENL][ENL]$/
# or
 m/^[ENL]{3}$/
but you still have the aforementioned problem of matching strings with repeated characters.

Mike's solution is probably what your are looking for (at least for three characters or less).

jaa
 
This one has been driving me crazy.
Mike's solution works, but what if it was a 6 character string? Maybe a regex wouldn't be the best option.
There's a quantifier - {MIN,MAX} which means "match MIN times but no more than MAX times" which might do the trick but I can't seem to get it to work.

anyone?

 
I came up with a two-regex solution. It works for any number of characters as long as each character is unique in the desired set.
Code:
if ($string =~ /^[ENL]{3}$/ and $string !~ /([ENL])(?=.*\1)/) {

    print "Match!\n";

}
jaa
 
Wow, justice, I really like that one. Something I'd of been hard pressed to come up with. Curious, what does the ?= do for you in this case? I'm not sure I've ever used the regex assertions before. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Curious, what does the ?= do for you in this case?

Um, in this case nothing. It works just as well without it.
Code:
$string !~ /([ENLZ]).*\1/
Thanks. I don't know why it stuck me first to do it that way.

jaa
 
I like Jaa's solution I think, neat. Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top