FAQ 6.16: How do I efficiently match many regular expressions at once? 
Author Message
 FAQ 6.16: How do I efficiently match many regular expressions at once?

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with every Standard Distribution of
Perl.

+
  How do I efficiently match many regular expressions at once?

    The following is extremely inefficient:

        # slow but obvious way

        while (defined($line = <>)) {

                if ($line =~ /\b$state\b/i) {  
                    print $line;
                    last;
                }
            }
        }                                        

    That's because Perl has to recompile all those patterns for each of the
    lines of the file. As of the 5.005 release, there's a much better
    approach, one which makes use of the new "qr//" operator:

        # use spiffy new qr// operator, with /i flag even
        use 5.005;


        while (defined($line = <>)) {

                print $line if $line =~ /$patobj/;
            }
        }

-

Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep comming up.  If you are some how irritated by
seeing these postings you are free to ignore them or add the sender
to your killfile.  If you find errors or other problems with these
postings please send corrections or comments to the posting email
address.

If you are not able to find this or other Perl documentation from
your installation you may access it via the web by following the
appropriate links from one of the addresses listed below.

    http://www.*-*-*.com/
    http://www.*-*-*.com/
    http://www.*-*-*.com/
    http://www.*-*-*.com/

Answers to questions about LOTS of other stuff, mostly not related to
Perl, can be found at


and in the many thousands of other useful Usenet news groups.

Please note that the FAQ text posted by this server has been modified
from that distributed in the stable Perl release.  It has been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ is available on request.

The perlfaq manual pages contain the following copyright notice.

  AUTHOR AND COPYRIGHT

    Copyright (c) 1997-1999 Tom Christiansen and Nathan
    Torkington.  All rights reserved.

    When included as an integrated part of the Standard
    Distribution of Perl or of its documentation (printed or
    otherwise), this work is covered under Perl's Artistic
    License.  For separate distributions of all or part of
    this FAQ outside of that, see the perlfaq manpage.

    Irrespective of its distribution, all code examples here
    are public domain.  You are permitted and encouraged to
    use this code and any derivatives thereof in your own
    programs for fun or for profit as you see fit.  A simple
    comment in the code giving credit to the FAQ would be
    courteous but is not required.

This work is provided in the hope that it will be useful but does
not represent a commitment of any kind on the part of the contributers,
authors or their agents.
--
    This space intentionally left blank



Sat, 22 Nov 2003 08:17:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. FAQ 6.16: How do I efficiently match many regular expressions at once?

2. FAQ 6.16: How do I efficiently match many regular expressions at once?

3. FAQ 6.16: How do I efficiently match many regular expressions at once?

4. FAQ 6.16: How do I efficiently match many regular expressions at once?

5. FAQ 6.16: How do I efficiently match many regular expressions at once?

6. FAQ 6.16: How do I efficiently match many regular expressions at once?

7. FAQ 6.16: How do I efficiently match many regular expressions at once?

8. FAQ 6.16 How can I do approximate matching?

9. FAQ 6.16 How can I do approximate matching?

10. FAQ 6.16 How can I do approximate matching?

11. FAQ 6.16 How can I do approximate matching?

12. FAQ 6.16 How can I do approximate matching?

 

 
Powered by phpBB® Forum Software