Greetings again good and saintly monks,

This is an interesting little regex problem that grew out of a recent class assignment:

Basically I want to take a line of text that contains values in Chinese national currency like this:

__DATA__ Hello all! Today I bought breakfast for 02.50 and lunch for 10.20 + Dinner was a real splurge at 21.00. __END__

And convert it and put it out in American dollars like this:

__DATA__ Hello all! Today I bought breakfast for 00.32 and lunch for 01.30 +. Dinner was a real splurge at 02.67. __END__

note that it is ok to keep the 0s on the left side of the numbers after the conversion.

I have managed to come up with something that looks like this:

my $string = <>; my $word; ### # I need help fixing this split function so that I can match the strin +gs below but # still keep the formatting from the original text. ### my @array = split(/*regex help needed here!*/, $string); while(@array){ $word = shift(@array); if($word = ~ /[0-9][0-9][.][0-9][0-9]/)){ ### #I have figured out the math to do the currency conversion ### print “$word”; } else{ print “$word”; } }

Basically I think it comes down to the fact I need help making the split function more intelligent. Is this possible, or am I asking too much from a simple regex?

Thanks for all help and opinions,

-mox

In reply to Crafting a regex for a split() function... by chinamox

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.