royskatt
royskatt

Reputation: 1210

Perl - Regex substitution does not work

My regex is supposed to substitute a date in a larger csv file with the word 'TIMESTAMP'.

Example data

TEXT12, TEXT312, 22.11.2013 13:30:16, abcd,

Code

while(<>) {                                                           
  s/[0-9]{2}.[0-9]{2}.[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}/TIMESTAMP/;
  print $_;                                                           
}    

 

I checked the regex with RegexBuddy, it matches there successfully, but in the Perl program it doesn't work. Tried with Perl 5.14 and Perl 5.18 on Linux and Windows, no success.

Where is the mistake?!

Upvotes: 0

Views: 85

Answers (2)

mpapec
mpapec

Reputation: 50637

Answer is in your csv file, you might have several white spaces between numbers so use \s+

s/[0-9]{2}\.[0-9]{2}\.[0-9]{4}\s+[0-9]{2}:[0-9]{2}:[0-9]{2}/TIMESTAMP/;

Also, when you want to match literal . char you have to escape it \. or use regex class for it [.]

Upvotes: 1

Filippo Lauria
Filippo Lauria

Reputation: 2064

You have to escape dots ..

Try this one:

s/[0-9]{2}\.[0-9]{2}\.[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}/TIMESTAMP/;

Upvotes: 1

Related Questions