Solved

Multi pattern match

Posted on 2001-07-31
5
592 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 11

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 11

Author Comment

by:tel2
ID: 6374742
ozo,

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

20 Experts available now in Live!

Get 1:1 Help Now