Discussion:
[rt-users] Regex in scrip won't match multi-line strings
Peter Nikolaidis
2017-02-27 15:48:18 UTC
Permalink
Hi all,

I'm trying to automatically resolve tickets that don't require any action.
In this example, I receive a backup notification. I know it's a backup
script result email based on the subject, and I know the backup was
successful (and therefore I can auto-resolve the ticket) if another regex
matches the body. When I match against the subject line alone, it works.
However, when I try to match against the mutli-line body, it never matches
(even though when I run the regex against the string in a test, it matches
as expected).

Are multi-line matches not an option, or do I need to test in another way?
This is an extract of the test I am running:

(($self->TicketObj->Subject =~ /Cron \<root\@blah\> \/usr\/bin\/s3cmd sync
\/var\/spool\/blah\/ s3\:\/\/somes3buckethere\//) &&
($self->TicketObj->Body =~ /Done\. Uploaded \d* bytes in \d\.\d seconds/))

Thank you,

Peter
--
Peter Nikolaidis
CISSP
<https://webportal.isc2.org/custom/CertificationVerificationResults.aspx?FN=Peter&LN=Nikolaidis&CN=453003>
, GCFE <https://www.giac.org/certified-professional/peter-nikolaidis/118082>
, GCIH <https://www.giac.org/certified-professional/peter-nikolaidis/118082>
, GPPA <http://www.giac.org/certified-professional/peter-nikolaidis/118082>
, GSNA <http://www.giac.org/certified-professional/peter-nikolaidis/118082>
Paradigm Consulting Co.
MA:617.517.2940 <(617)%20517-2940> * NH:603.676.7119 <(603)%20676-7119>
* VT:802.234.6368 <(802)%20234-6368>
http://pa.radigm.com
Matt Zagrabelny
2017-02-27 15:57:52 UTC
Permalink
Post by Peter Nikolaidis
Hi all,
I'm trying to automatically resolve tickets that don't require any action.
In this example, I receive a backup notification. I know it's a backup
script result email based on the subject, and I know the backup was
successful (and therefore I can auto-resolve the ticket) if another regex
matches the body. When I match against the subject line alone, it works.
However, when I try to match against the mutli-line body, it never matches
(even though when I run the regex against the string in a test, it matches
as expected).
Are multi-line matches not an option, or do I need to test in another way?
From:

http://perldoc.perl.org/perlre.html

"""
Modifier m

Treat the string as multiple lines. That is, change "^" and "$" from
matching the start of the string's first line and the end of its last
line to matching the start and end of each line within the string.
"""
I almost always use /xms for my REs. This allows for significant
readability - at the cost of significant number of lines. Check out
Damian Conway's "Perl Best Practices" Regular Expression chapter.

-m
Peter Nikolaidis
2017-03-03 15:30:02 UTC
Permalink
Apparently I'm still doing something wrong and regex is kicking my butt.

Sample message body:
-----------------------------------------
* Stopping Asterisk PBX: asterisk
...done.
* Starting Asterisk PBX: asterisk
...done.
-----------------------------------------

Patterns I've tried:
-----------------------------------------
1:
(($self->TicketObj->Subject =~ /Cron \<root\@pbx\> \/etc\/init\.d\/asterisk
restart/) && # Successful cron jobs.
($self->TicketObj->Body =~ /\* Starting Asterisk PBX\:
asterisk\n\.\.\.done\./m)

2:
(($self->TicketObj->Subject =~ /Cron \<root\@pbx\> \/etc\/init\.d\/asterisk
restart/) && # Successful cron jobs.
($self->TicketObj->Body =~ /\* Starting Asterisk PBX\:
asterisk.*\.\.\.done\./ms))
3:
(($self->TicketObj->Subject =~ /Cron \<root\@pbx\> \/etc\/init\.d\/asterisk
restart/) && # Successful cron jobs.
($self->TicketObj->Body =~ /^\* Starting Asterisk PBX\:
asterisk.*\.\.\.done\.$/ms)) ||
-----------------------------------------

Any ideas?

Thanks,

Peter
Post by Peter Nikolaidis
Post by Peter Nikolaidis
Hi all,
I'm trying to automatically resolve tickets that don't require any
action.
Post by Peter Nikolaidis
In this example, I receive a backup notification. I know it's a backup
script result email based on the subject, and I know the backup was
successful (and therefore I can auto-resolve the ticket) if another regex
matches the body. When I match against the subject line alone, it works.
However, when I try to match against the mutli-line body, it never
matches
Post by Peter Nikolaidis
(even though when I run the regex against the string in a test, it
matches
Post by Peter Nikolaidis
as expected).
Are multi-line matches not an option, or do I need to test in another
way?
http://perldoc.perl.org/perlre.html
"""
Modifier m
Treat the string as multiple lines. That is, change "^" and "$" from
matching the start of the string's first line and the end of its last
line to matching the start and end of each line within the string.
"""
I almost always use /xms for my REs. This allows for significant
readability - at the cost of significant number of lines. Check out
Damian Conway's "Perl Best Practices" Regular Expression chapter.
-m
Matt Zagrabelny
2017-03-03 15:46:05 UTC
Permalink
Post by Peter Nikolaidis
Apparently I'm still doing something wrong and regex is kicking my butt.
-----------------------------------------
* Stopping Asterisk PBX: asterisk
...done.
* Starting Asterisk PBX: asterisk
...done.
-----------------------------------------
-----------------------------------------
restart/) && # Successful cron jobs.
asterisk\n\.\.\.done\./m)
restart/) && # Successful cron jobs.
asterisk.*\.\.\.done\./ms))
restart/) && # Successful cron jobs.
asterisk.*\.\.\.done\.$/ms)) ||
-----------------------------------------
Any ideas?
#!/usr/bin/perl

use strict;
use warnings;

my $sample_message_body = <<EOF;
* Stopping Asterisk PBX: asterisk
...done.
* Starting Asterisk PBX: asterisk
...done.
EOF

my $sample_message_body_2 = <<EOF;
* Stopping Asterisk PBX: asterisk.
...done.
* Starting Asterisk PBX: asterisk.
...done.
EOF

my $re = qr{
^\*\s+Stopping\s+Asterisk\s+PBX:\s+asterisk$
\n
^\.\.\.done\.$
\n
^\*\s+Starting\s+Asterisk\s+PBX:\s+asterisk$
\n
^\.\.\.done\.$
}xms;

if ($sample_message_body =~ $re) {
print "MATCHES!\n";
}
else {
print "NOPE!\n";
}

if ($sample_message_body_2 =~ $re) {
print "MATCHES!\n";
}
else {
print "NOPE!\n";
}

-m
---------
RT 4.4 and RTIR Training Sessions https://bestpractical.com/training
* Paris - April 24-26, 2017

Loading...