I like to keep my regexes simple.

Strip a dot and some digits from the end of a string:

my $str = '02/17/2003 11:44:19.123'; $str =~ s/\.\d+$//; print "$str\n";
You say you are struggling with regexes, so here is the explanation:

We could *try* to say s/.\d+$// but it happens that the dot is magic (matches most anything). So to match a dot we have to "escape" it with the backslash. Like this: \.

Next comes \d which means "any digit". The plus after it means "one or more". Like this: \d+

Then we have the $   When $ is used at the end of a regex, it means "anchor this match to the end of the string" (there are some nuances here we won't bother with). Actually, in this case we don't need the $ anchor -- we know that there is only one place in the string that will match "a dot followed by some digits". But it is perhaps a kindness to the next human who looks at the code to provide this visual clue that the match is expected to occur at the end of the string. So now we have: \.\d+$

We put this regex snippet into a substitution regex: s/ / / But we leave the second half empty. This means "whatever you matched in the first half of the s///, replace it with nothing at all".

So reading s/\.\d+$// straight off the page, we could translate it as: match a literal dot followed by some digits anchored to the end of the string and replace them with nothing.

Hope that helps.

------------------------------------------------------------
"Perl is a mess and that's good because the
problem space is also a mess.
" - Larry Wall


In reply to Re: Pattern Match/Trim Variables. by dvergin
in thread Pattern Match/Trim Variables. by LostS

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.