Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multi pattern match

Posted on 2001-07-31
5
Medium Priority
?
620 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 90 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

670 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