Solved

Multi pattern match

Posted on 2001-07-31
5
595 Views
Last Modified: 2008-02-01
UNIX has a "-E" switch to allow its grep utility to match multiple patterns at once, eg:
  grep -E "abc|def|ghi|jkl" infile
will match all lines in infile which contain "abc" or "def", etc.

I know Perl has a "grep" command, and I know it can also do pattern matching with the "=~" operator, but how can I write CONCISE Perl code to match multiple patterns with the:
1. grep command?
2. "=~" operator?

Thanks.
0
Comment
Question by:tel2
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 6339110
print grep /abc|def|ghi|jkl/ <>;
0
 
LVL 84

Expert Comment

by:ozo
ID: 6339119
print grep /abc|def|ghi|jkl/,<>;
0
 
LVL 12

Author Comment

by:tel2
ID: 6343456
ozo,

Is this an answer to both questions, or just #1?  If just #1, are there any other commands (eg: "=~") which allow for multiple pattern matching, if so, please give examples.

What does the "<>" mean exactly?
0
 
LVL 84

Accepted Solution

by:
ozo earned 30 total points
ID: 6345384
while( defined($strimg = <>) ){
    print $string if $string =~ /abc|def|ghi|jkl/;
}


perldoc -f readline
                 readline EXPR
                         Reads   from  the  filehandle  whose  typeglob  is
                         contained in EXPR.  In scalar context,  each  call
                         reads and returns the next line, until end-of-file
                         is reached, whereupon the subsequent call  returns
                         undef.   In  list context, reads until end-of-file
                         is reached and returns a list of lines.  Note that
                         the  notion  of  ``line'' used here is however you
                         may    have    defined    it    with     $/     or
                         $INPUT_RECORD_SEPARATOR).  See the section on "$/"
                         in the perlvar manpage.
                         When $/ is set to undef,  when  readline()  is  in
                         scalar context (i.e. file slurp mode), and when an
                         empty file is read, it returns '' the first  time,
                         followed by undef subsequently.
                         This  is  the  internal  function implementing the
                         <EXPR> operator, but you can use it directly.  The
                         <EXPR> operator is discussed in more detail in the
                         I/O Operators entry in the perlop manpage.
                             $line = <STDIN>;
                             $line = readline(*STDIN);           # same thing

0
 
LVL 12

Author Comment

by:tel2
ID: 6374742
ozo,

Thanks for that.
So, are you saying that "<>" is the same as "readline"?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now