Matching a word or Pattern in perl

R

Raj Sathgunam

Hi ,

I have to match either one of the below output from a log file.
Match "No Activity" OR XYZ:10 ABC:30 MNO:10 PQR:5 .
I could get pattern for second one. How can i combine the patterns to
match either one of them. Id this valid
/No Activity/||/.+\sXYZ:(\d+)+\s+ABC:(\d+)+\s+MNO:(\d+)+\s+PQR:(\d+)/

Thanks
 
G

Gunnar Strand

Hi Raj,

Raj said:
Hi ,

I have to match either one of the below output from a log file.
Match "No Activity" OR XYZ:10 ABC:30 MNO:10 PQR:5 .
I could get pattern for second one. How can i combine the patterns to
match either one of them. Id this valid
/No Activity/||/.+\sXYZ:(\d+)+\s+ABC:(\d+)+\s+MNO:(\d+)+\s+PQR:(\d+)/

I'm no expert on re, but yes, it would be valid to have that expression,
but only if the string is in $_. Otherwise each pattern match would
need the variable as well:

$line =~ /No Activity/ || $line =~
/.+\sXYZ:(\d+)+\s+ABC:(\d+)+\s+MNO:(\d+)+\s+PQR:(\d+)/

You will find that the pluses after your number matches are superfluous:

(\d+)+ can be written (\d+)

If your log file contains many "No Activity" lines, then you will
probably find it to run faster if you use ordinary string comparison
for the no activity lines, eg.:

'No Activity' eq $_

or, if you haven't used 'chop' or 'chomp' on the string:

'No Activity\n' eq $_

(I am assuming the 'No Activity' is the only text on the line.)

Kind Regards,

/Gunnar
 
K

Kevin Collins

Hi Raj,



I'm no expert on re, but yes, it would be valid to have that expression,
but only if the string is in $_. Otherwise each pattern match would
need the variable as well:

$line =~ /No Activity/ || $line =~
/.+\sXYZ:(\d+)+\s+ABC:(\d+)+\s+MNO:(\d+)+\s+PQR:(\d+)/

Yes, but the OP asked for a way to combine them, which is not what you are
doing (and neither is his suggestion) - that is 2 separate commands. This
could be used instead:

/No Activity|.+\sXYZ:(\d+)+\s+ABC:(\d+)+\s+MNO:(\d+)+\s+PQR:(\d+)/
^

Note the '|' to use alternation...
You will find that the pluses after your number matches are superfluous:

(\d+)+ can be written (\d+)

If your log file contains many "No Activity" lines, then you will
probably find it to run faster if you use ordinary string comparison
for the no activity lines, eg.:

'No Activity' eq $_

or, if you haven't used 'chop' or 'chomp' on the string:

'No Activity\n' eq $_

(I am assuming the 'No Activity' is the only text on the line.)

Kind Regards,

/Gunnar

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top