user1666605
user1666605

Reputation: 3

Using Reg Exp to replace string that is unique in Perl

I am trying to write this script in Perl to check for a unique string inside the variable $summary from my query results. There is multiple similar entries like this but part of them are unique (db1node1web1 could be different nodes but same result type). This is only a small part of the code to check if I using Reg Ex correctly (not true currently).

I included the code below:

$summary = "NFW - CRITICAL CPU_Load_db1node1web1 CRITICAL - load average: 114.49, 48.55, 29.17 Sept 01 00:10:16 PDT 2012";
if ( $summary =~ "NFW - CRITICAL CPU_Load_[a-z]* CRITICAL - load average: 114.49, 48.55, 29.17 Sept 01 00:10:16 PDT 2012") {
    print "True\n";
}else {
    print "False\n";
}

I am very bad with Perl and trying to get this working so I can dissect my query results.

Upvotes: 0

Views: 108

Answers (4)

Anil
Anil

Reputation: 2554

It's been a few years since I've coded in perl, but IIRC, don't you have to specify whether it's a match or substitution? And you need to include numbers

I believe your code should be:

$summary = "NFW - CRITICAL CPU_Load_db1node1web1 CRITICAL - load average: 114.49, 48.55, 29.17 Sept 01 00:10:16 PDT 2012";
if ( $summary =~ m/NFW - CRITICAL CPU_Load_[a-z0-9]* CRITICAL - load average: 114.49, 48.55, 29.17 Sept 01 00:10:16 PDT 2012/) {
    print "True\n";
}else {
    print "False\n";
}

Reference: http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

Upvotes: 2

Andrew Cheong
Andrew Cheong

Reputation: 30273

If you only need to match a substring, then specify only that, e.g.

$summary =~ /NFW - CRITICAL CPU_Load_/

Upvotes: 3

perreal
perreal

Reputation: 97948

You need to use slashes for quoting regular expressions instead of double quotes. Also, your input db1node1web1 contains digits as well as characters:

if ( $summary =~ /NFW - CRITICAL CPU_Load_[a-z0-9]* CRITICAL - load average: 114.49, 48.55, 29.17 Sept 01 00:10:16 PDT 2012/) {

  print "True\n";

}else {

  print "False\n";

}

Upvotes: 0

choroba
choroba

Reputation: 241898

[a-z] does not include numbers. Your example string contains several 1's. Adjust the class to

[a-z0-9]

Upvotes: 0

Related Questions