EvoTalk

07 十一月, 2007

perl regex : grep

Posted by: asd In: Code Snippet| Perl ()

PERL:
  1. #!/usr/bin/perl
  2. # perl-grep3.pl
  3. my $pattern = shift @ARGV;
  4. #qr// is a regex quoting operator that stores my regex in a scalar
  5. #qr// compiles the pattern so it ready to use
  6. #eval operator around the qr// to catch the error
  7. my $regex = eval { qr/$pattern/ };
  8. die "Check your pattern! $@" if $@;
  9. while( <> )
  10. {
  11. #The $& variable holds the portion of the string that matched
  12. print "$_\t\tmatched>>>$&<<<\n" if m/$regex/;
  13. }

ex:

perldoc -t perl | perl-grep3.pl "\b(\S)\S\1\b"

output:

perl587delta Perl changes in version 5.8.7
matched >>>.8.<<<

Tags:

Releated Posts



No Responses to "perl regex : grep"

Comment Form