Home: Perl Programming Help: Regular Expressions:
need some help on this 'simple' case



jerseyguy
New User

Oct 4, 2006, 8:07 AM


Views: 8400
need some help on this 'simple' case

hi, all

not sure it's a intermediate question, but i desperately need some help:

i need a regular expression to match a string which includes 'abc' except 'abc123' and 'abc456':

for example:

'abc' (match)

'abcdefg' (match)

'abc123' (not match)

'abc123456' (not match)

'abc456' (not match)

'abc456789' (not match)

'abcd123' (match)



thanks in advance


KevinR
Veteran


Oct 4, 2006, 10:02 AM


Views: 8399
Re: [jerseyguy] need some help on this 'simple' case

posting the same question in multiple forums is very obnoxious. If you want help with this question please post the code you have already been trying to use.
-------------------------------------------------


jerseyguy
New User

Oct 4, 2006, 10:57 AM


Views: 8397
Re: [KevinR] need some help on this 'simple' case

sorry KevinR, the situation is kind of urgent here.

here is my code:

$string1 = "abc123";
#$string1 = "msadc/msadcs.dll/navigatorobject";

if ($string1 =~ m/abc.*[^(123|456)]/) {
print "\yes\n";
}
else{
print "\nno\n";
}


it doesn't work, what i need is to match any string which includes 'abc' except 'abc123' and 'abc456'.


KevinR
Veteran


Oct 4, 2006, 1:57 PM


Views: 8395
Re: [jerseyguy] need some help on this 'simple' case

instead of:


Code
m/abc.*[^(123|456)]/


use:


Code
m/abc(?!123|456)/



The '?!' extension lets you specify what should not follow your pattern. You were using a negated character class which can maybe be used in some cases for this type of matching but the '?!' extension is better for what you are trying to do.

I hope I am not doing your school work for you. Frown
-------------------------------------------------


(This post was edited by KevinR on Oct 4, 2006, 1:57 PM)