[Chicago-talk] regural expression question?

Joshua McAdams joshua.mcadams at gmail.com
Thu Mar 30 07:01:10 PST 2006


> > I have a little script that finds the line which
> > contains a six digit number begining with 1 or
> > greater.  I know that $_ represents the line,
> > however, I am having trouble figuring out how to
> > save that number once it's found (without everything
> > else in the line).
> >
> >  #!/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";
> >      }
> >  }
> >

Put some parenthesis around the expression...

if ( /\b([123456789][0-9][0-9][0-9][0-9][0-9])\b/ ) {
    print "Found\n" . $1 . "\n";
}

I think that you could tighten up the expression a little too...

if ( /\b([1-9]\d{5})\b/ ) {
    print "Found\n" . $1 . "\n";
}


More information about the Chicago-talk mailing list