Did you ever happened to have a sudden enlightenment and discover that you, stupid, was struggling to find something deeply wrong, which couldn't be found in any way?
That's what I happened to experience while coding a simple script to rename a bunch of html files containing web-emails, according to name of the sender and date.
I use to check my email on the web, then dump each message as a single html file and delete the whole folder content on the website.
My old script was a mess of regexes, difficult to maintain and adjust to the frequent changes in my webmail service's frequent html structure changes; a question in "Seekers of Perl Wisdom" solved my doubts and made me discover the good HTML::TokeParser and the killing Date::Manip, which turned out to be far the best solution I ever expected to find.
But something didn't work...I mean, there were some specific files which "Date" field seemed to be haunted...the script was correctly parsing html and storing the string containing the Date, but then Date::Manip failed to convert it in the format I like, i.e.:"%y%m%d". I started thinking that the html could be somewhat different between each file, and checked.
nothing.
Then I spent the following 30 minutes reading word by word the Date::Manip manpage, finding that it was correctly used.
But some files were still haunted, and the date seemed to disappear as soon as I tried to convert it with the magic module...
Then, I started inspecting the Date strings, which usually look like this one:
Wed, 28 Jul 2004 9:12 PM ( 14 hours 34 mins ago )
and the regex which gets rid of that non-standard useless reminder between parentheses:
$value=~s/M.*$/M/;
What did I mean? I wanted to replace PM or AM and the following characters with just PM or AM. So I thought it was a good idea to refer just to the M, which was the character in common, and build my killer regex on it.
No way, men, with some files it failed, and the whole date disappeared in a puff of smoke...

until...

...until I discovered that sometimes it's Monday!

useless to say that my regex now is:
$value=~s/[AP]M.*$//;


Edited: i forgot an M in my first regex!

janitored by ybiC: Retitle from one-word "Epiphany" because one-word nodetitles hinder site search


In reply to Regex Epiphany by Sigmund

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.