in reply to Scan Files - Match String

Hi deelinux,

The reason your code isn't working is the way you've saved your regex. In strings surrounded by double quotes, backslash escapes like \d and \s mean something different than in regexes. In fact, \d and \s are not valid in double-quoted strings, only regexes (for details, see Quote Like Operators, perlrebackslash and Gory details of parsing quoted constructs). You can see this if you use Data::Dumper to look at the variable (see also the Basic debugging checklist):

my $match1 = "^$datestring\s+\d{2}:\d{2}:\d{2},\d{3}\s+-\s+\d{2}:\d{2} +:\d{2},\d{3}\s+$SLEVEL1"; use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper($match1); __END__ $VAR1 = "^2017-01-04s+d{2}:d{2}:d{2},d{3}s+-s+d{2}:d{2}:d{2},d{3}s+ERR +OR";

Also, if you had turned on warnings, you would have gotten warnings about this, which is why in my other post I recommended Use strict and warnings!

Personally, I think the best way to fix this is to use qr//. If I do the following, your code starts working:

my $match1 = qr/^$datestring\s+\d{2}:\d{2}:\d{2},\d{3}\s+-\s+\d{2}:\d{ +2}:\d{2},\d{3}\s+$SLEVEL1/;

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Scan Files - Match String
by deelinux (Novice) on Jan 04, 2017 at 18:47 UTC

    Hello Hauke and everyone else that commented, many thanks all. I have taken note about the correct way to submit a questions and how to do regex the right way. In the end I really liked the easier method by Hauke,  if (/\Q$datestring/ && /ERROR|WARN/) This worked a treat, so thanks again, I'll play around with the Regex way and see if I can get that working. :-)