0

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?!

2 Answers 2

1

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/;
Sign up to request clarification or add additional context in comments.

1 Comment

@royskatt in your case . and \. both match dot char.
1

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 [.]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.