[TriLUG] OT - Reg Exp question.

Andrew Perrin clists at perrin.socsci.unc.edu
Wed Nov 7 15:58:37 EST 2007


This is an order-of-operations problem. ! is higher-order than =~, but =~ 
and !~ are equivalent order. So your first expression is testing the 
lack-of-match you contemplate, but the second expression is first 
evaluating

! $tmptoke

and then testing whether the outcome of that matches the regex:

----
aperrin at che:~$ perl -de1

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1):   1
   DB<1> $tmptoke = 0

   DB<2> print ! $tmptoke
1
----

A nice alternative to your if (! $tmptoke) is also:

unless ($tmptoke =~ /^[0-9][-0-9,]*$/ ) {


all this assumes you're using perl - if not, I'm probably wrong :)

Andy




----------------------------------------------------------------------
Andrew J Perrin - andrew_perrin (at) unc.edu - http://perrin.socsci.unc.edu
Associate Professor of Sociology; Book Review Editor, _Social Forces_
University of North Carolina - CB#3210, Chapel Hill, NC 27599-3210 USA



On Wed, 7 Nov 2007, Scott Lambdin wrote:

> With
> if ( $tmptoke !~ /^[0-9][-0-9,]*$/  ) {
>
> the 0's get matched.  With
>
> if ( ! $tmptoke =~ /^[0-9][-0-9,]*$/ ) {
>
> they do not.
>
> Thanks.
>
> --Scott
>
>
> On 11/7/07, James Olin Oden <james.oden at gmail.com> wrote:
>>
>> On 11/7/07, Scott Lambdin <lopaki at gmail.com> wrote:
>>> Hi -
>>>
>>> Can anyone tell me why /^[0-9][-0-9,]*$/  fails to match a lone 0?  It
>> will
>>> match a 3 or a 5, but not a zero.  I must have fallen asleep at some
>> point
>>> in Regular expressions class.
>>>
>> It works for me also.  It should match anything starting with a digit
>> optionally followed by digits, dashes and commas, so the following
>> should match:
>>
>>    0
>>    10
>>    01-
>>    0-1
>>    111,222,555-766
>>
>> as an example but:
>>
>> a0
>> 01a
>> 0-a
>>
>> would not match.
>>
>> What is the bigger context of your regex (i.e. what code surrounds it)?
>>
>> Cheers...james
>>> --Scott
>>> --
>>> TriLUG mailing list        :
>> http://www.trilug.org/mailman/listinfo/trilug
>>> TriLUG Organizational FAQ  : http://trilug.org/faq/
>>> TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
>>>
>> --
>> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
>> TriLUG Organizational FAQ  : http://trilug.org/faq/
>> TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
>>
> -- 
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG Organizational FAQ  : http://trilug.org/faq/
> TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
>



More information about the TriLUG mailing list