[Chicago-talk] regural expression question?

Steven Lembark lembark at wrkhors.com
Thu Mar 30 07:26:13 PST 2006


 >>  #!/usr/bin/perl -w
 >>
 >>  @ARGV = "/home/richard/test/out.txt";
 >>  while (<>) {
 >>      if ( /\b[123456789][0-9][0-9][0-9][0-9][0-9]\b/
 >> ) {
 >>          print "Found\n" . $_ . "\n";
 >>      }
 >>  }

Use parens and assign the result:

     my( $whole_number, $first_digit )
     = m{ \b ( ( [1-9] ) \d+ ) \b }x;
             1 2       2     1

or

     if( m{ \b ( ( [1-9] ) \d+ ) \b }x )
     {
         my $whole_number = $1;
         my $first_digit  = $2;
     }

The capturing paren's store their result in the
order of opening. In the example above, the whole
set of digits is enclosed in the first set, the
first digit in the second. That'll give you $1
and $2 if there is a match or return the two in
that order.


-- 
Steven Lembark                                         85-09 90th Street
Workhorse Computing                                  Woodhaven, NY 11421
lembark at wrkhors.com                                      +1 888 359 3508


More information about the Chicago-talk mailing list