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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex Character Class bug?

Status
Not open for further replies.

Defcon5

Programmer
Jun 19, 2005
2
NL
I can't figure out why the following does not work.

Code:
if('CCTAGG' =~ /[^A]/) {
	print "match";
}

I would expect that it would not match because the string has a A in it. But it DOES match. Can someone explain this to me?

When I remove the carrot (^), than it also matches. Which I expect because the string contains a "A".

When I change the "A" to a "X", it doesn't match anymore, as expected.

Thanks,

Jesse
 
That regexp isn't doing what you think it should be doing. It will match if the string `CCTAGG' contains *any* character that isn't an `A'. Since it contains 5 such characters, it'll match. What you want is:
Code:
if('CCTAGG' !~ /A/ ) {

}
 
To get the effect I think you want you need to change your code to this:
Code:
if('CCTAGG' [red]![/red]~ /A/) {
    print "match";
}
Looks like more genetic code. ;-)

Trojan.
 
Thanks for the replies.

The problem was that Regex stops at the first hit in the previous example. So it saw a "C" as first character, and than checked it against "[^A]" and immediately stopped searching further.


I fixed it by doing this:
Code:
if('CCTAGG' =~ /^[^A]*$/) {
    print "match";
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top