RegExp Help

Discussion in 'Perl Misc' started by Indigo5, Dec 6, 2004.

  1. Indigo5

    Indigo5 Guest

    This may be a simple question, but how would I use a regular expression to
    convert reals to integers whenever possible. For example, if I had the
    following lines

    25.010 36.5 20.00
    22.3 19. 35.


    I would want those converted to

    25.010 36.5 20
    22.3 19 35

    I know I can use something like:

    s/\.$//g; to get rid of decimal points at the end of the string, but how do
    I get rid of trailing zeros after the decimal point?

    Would it be something like

    s/\.(\d+)0$//g; ?
    Indigo5, Dec 6, 2004
    #1
    1. Advertising

  2. Indigo5

    Scott Bryce Guest

    Indigo5 wrote:

    > This may be a simple question, but how would I use a regular expression to
    > convert reals to integers whenever possible. For example, if I had the
    > following lines
    >
    > 25.010 36.5 20.00
    > 22.3 19. 35.
    >
    >
    > I would want those converted to
    >
    > 25.010 36.5 20
    > 22.3 19 35


    No regex needed:

    use strict;
    use warnings;

    my $x1 = '1.9';
    my $x2 = '2.00';
    my $x3 = '3.';

    print "x1 = $x1, x2 = $x2, x3 = $x3\n";

    $x1 += 0;
    $x2 += 0;
    $x3 += 0;

    print "x1 = $x1, x2 = $x2, x3 = $x3\n";
    Scott Bryce, Dec 6, 2004
    #2
    1. Advertising

  3. Indigo5

    Andrew Hamm Guest

    Indigo5 wrote:
    > This may be a simple question, but how would I use a regular
    > expression to convert reals to integers whenever possible. For
    > example, if I had the following lines
    >
    > 25.010 36.5 20.00
    > 22.3 19. 35.
    >
    >
    > I would want those converted to
    >
    > 25.010 36.5 20
    > 22.3 19 35


    From your sample output, you do not appear to want to convery 25.010 into
    25.01 therefore I think you want this:

    s/(\.0*)(?!\d)/' ' x length $1/eg;

    I am guessing also that you want to maintain the column structure,
    therefore I'm replacing the removed characters with the same amount of
    spaces. If that is not important, then it's

    s/(\.0*)(?!\d)//g;
    Andrew Hamm, Dec 7, 2004
    #3
  4. Indigo5

    Anno Siegel Guest

    BigDaDDY <> wrote in comp.lang.perl.misc:
    > Andrew,
    >
    > My bad..I did want 25.010 to be converted to 25.01. Nice catch.
    >
    > How does this change the regexp you provided?


    "I mis-stated my question, so I can't use your reply. Send another."

    Have you even made an attempt to correct it yourself?

    [TOFU snipped]

    Anno
    Anno Siegel, Dec 7, 2004
    #4
  5. Indigo5

    Scott Bryce Guest

    BigDaDDY wrote:
    > Andrew,
    >
    > My bad..I did want 25.010 to be converted to 25.01. Nice catch.
    >
    > How does this change the regexp you provided?


    Is there a reason why you insist on using a regex for a task that does
    not require one?
    Scott Bryce, Dec 7, 2004
    #5
  6. Indigo5

    Andrew Hamm Guest

    Scott Bryce wrote:
    > BigDaDDY wrote:
    >> Andrew,
    >>
    >> My bad..I did want 25.010 to be converted to 25.01. Nice catch.
    >>
    >> How does this change the regexp you provided?

    >
    > Is there a reason why you insist on using a regex for a task that does
    > not require one?


    My guess is that he wants to retain the data in the original string;
    perhaps massaging and cleaning up a report from some other external source
    that he may not be able to fix.
    Andrew Hamm, Dec 7, 2004
    #6
  7. Indigo5

    Andrew Hamm Guest

    BigDaDDY wrote:
    > Andrew,
    >
    > My bad..I did want 25.010 to be converted to 25.01. Nice catch.
    >
    > How does this change the regexp you provided?


    ok - welll, the way I think about it;

    we want a string of zero's followed by a non-digit.

    Adding in the recognition of the optional leading dot; well, it's an
    optional leading dot! So the pattern starts to look like

    (\.?0*)(?!\d)

    the negative lookahead should ensure that this is recognised only at the
    tail end of a number.

    However, if there are any integers in the listing - ie without the dot,
    then they might be destroyed; for example 100 would be converted to 1

    If that is a risk; it will probably get much uglier. If it's not a
    problem, I'll save time and not think about it unless you say it's needed.
    Could get interesting solving that one and still keep it elegant.
    Andrew Hamm, Dec 7, 2004
    #7
  8. Indigo5

    Scott Bryce Guest

    Andrew Hamm wrote:

    > My guess is that he wants to retain the data in the original string;
    > perhaps massaging and cleaning up a report from some other external source
    > that he may not be able to fix.


    That still wouldn't require a regex.

    my $var_1 = '2.0';
    my $var_2 = $var_1 + 0;

    Use $var_2 in the report, and you still have the original data in $var_1.
    Scott Bryce, Dec 8, 2004
    #8
  9. Indigo5

    Andrew Hamm Guest

    Scott Bryce wrote:
    >
    > That still wouldn't require a regex.
    >
    > my $var_1 = '2.0';
    > my $var_2 = $var_1 + 0;
    >
    > Use $var_2 in the report, and you still have the original data in
    > $var_1.


    Indigo5 submitted this data

    > 25.010 36.5 20.00
    > 22.3 19. 35.


    so that looks like a pre-existing text file to me. That's why I think it's
    too late to perform a fix where you suggest. Still, only Indigo knows for
    sure, and it would be nice if Indigo could satisfy our curiosity with a
    bit of info about where it's coming from - if you think it's important
    enough to clear up. Otherwise assumptions rule the day.

    If the task is a report massage then (s)he is not in control of the code
    generating it (at a guess). If (s)he is writing the original text file
    then of course fixing the source of the text files is more appropriate.
    Andrew Hamm, Dec 8, 2004
    #9
  10. Indigo5

    Andrew Hamm Guest

    Andrew Hamm wrote:
    >
    > If the task is a report massage then (s)he is not in control of the


    hmm - other reply was handled "BigDaddy" so I guess Indigo5 aka Big Daddy
    is indeed a he.
    Andrew Hamm, Dec 8, 2004
    #10
  11. Indigo5

    Scott Bryce Guest

    Andrew Hamm wrote:

    > so that looks like a pre-existing text file to me.


    No doubt.

    > That's why I think it's
    > too late to perform a fix where you suggest.


    Huh?

    I'm not suggesting fixing the file. I am only suggesting an easier way
    to drop trailing zeroes to the right of the decimal than a regex.

    > Still, only Indigo knows for
    > sure, and it would be nice if Indigo could satisfy our curiosity with a
    > bit of info about where it's coming from


    That doesn't matter. Once his script has the data, the source of the
    data is irrelevant, if all he wants to do is strip trailing zeroes to
    the right of the decimal.

    Or did I miss something?

    OK, taking another look at the original post, it looks like he wants to
    retain the formatting of each line. If that is the case, you have a point.
    Scott Bryce, Dec 8, 2004
    #11
  12. Indigo5

    Andrew Hamm Guest

    Scott Bryce wrote:
    >
    > OK, taking another look at the original post, it looks like he wants
    > to retain the formatting of each line. If that is the case, you have
    > a point.


    yup - that was my interpretation. It struck a chord for me because in the
    business data world, that's the sort of crap we sometimes have to do;
    process outside data which can be in as bad a format as a captured report.
    I sincerely hope that XML will soon make report-scraping, CSV, "unload
    files" and plain text files nothing but a fuzzy memory.

    I'm also kind of wishing that there might be integers in the report which
    need protecting against loss of trailing zeros. I'm letting my brain
    "background" a solution because it's interesting. A bit of progress has
    already popped up. I'm not sure it won't be another stepup of difficulty,
    but am not willing to put the time in to play yet. I really shouldn't be
    posting messages either...
    Andrew Hamm, Dec 8, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    152
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    466
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    343
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    185
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    170
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page