Loading ...
Sorry, an error occurred while loading the content.

Re: [NTS] Trying to perfect RegExp to match various numbers

Expand Messages
  • Alec Burgess
    cc ntb-clips (see note at end) ... Following will enforce 5 digits (zero-padded) before optional decimal and 4 after H=test B3-30 leading / trailing zeros ;
    Message 1 of 14 , Mar 30, 2011
    • 0 Attachment
      cc ntb-clips (see note at end)

      On 2011-03-30 15:21, mycroftj wrote:
      > I'm terribly sorry for not being clear. I branched into quite a few
      > directions at once.
      >
      > The goal was to pad all numbers in a document with spaces or zeros so
      > they would sort correctly.
      >
      > Instead of writing a clip, I thought it might be done in one mighty
      > regexp replace (777->000777 and 77->000077) but that would involve
      > calculating lengths and taking decimal points into consideration so I
      > don't see how that can be done. OR CAN IT?
      >
      >
      Following will enforce 5 digits (zero-padded) before optional decimal
      and 4 after

      H=test B3-30 leading / trailing zeros
      ; Alec Burgess 2011-03-30
      ; currently enforces 5 digits before (optional) decimal and 4 after
      ^!replace "\b(\d+)\.?(\d*)\b" >> "00000$1.$2===0000" rwais
      ^!replace "===" >> "" rwais
      ^!replace "\b0*(\d{5})\.(\d{4})0*\b" >> "$1.$2" rwais

      Note - I wanted the first replace to be just "00000$1.$20000" but I
      haven't figured out how to prevent clip replace from confusing $2 with
      $20 - (ie. non-existent 20th or 20000th sub-pattern.

      Does anyone know how to do this? As is just make sure "===" is any
      string which does not exist in the input.

      Note - adding line ^!replace "\.0*\b" >> "" rwais to above will
      eliminate decimal padding after unnecessary decimal point.

      sample input
      1
      123
      123.
      123.1
      123.123

      resulting output
      00001.0000
      00123.0000
      00123.0000.
      00123.1000
      00123.1230

      btw: ntb-clips group would be a better place for this discussion that
      ntb-scripts. As originally intended ntb-scripts was for discussion of
      things like using Perl and JavaScript in clip code. Its readership is
      much less than the ntb-clips though I assume everyone who follows
      ntb-scripts also follows ntb-clips :-)

      Regards ... Alec (buralex@gmail& WinLiveMess - alec.m.burgess@skype)
    • Eb
      Alec, I m not sure this will work, but the variable ought to break up the output pattern: ^!replace b( d+) .?( d*) b 00000$1.$2^%empty%0000 rwais
      Message 2 of 14 , Apr 1, 2011
      • 0 Attachment
        Alec,

        I'm not sure this will work, but the variable ought to break up the output pattern:

        ^!replace "\b(\d+)\.?(\d*)\b" >> "00000$1.$2^%empty%0000" rwais


        Cheers


        Eb

        --- In ntb-scripts@yahoogroups.com, Alec Burgess <buralex@...> wrote:
        ...
        > Does anyone know how to do this? As is just make sure "===" is any
        > string which does not exist in the input.
      • Alec Burgess
        ... It does allow the $2 to be substituted but ^%empty% does not appear to get translated. I get results like this: 45.6 == 0000045.6^%empty%0000 -- Regards
        Message 3 of 14 , Apr 1, 2011
        • 0 Attachment
          On 2011-04-01 16:08, Eb wrote:
          >
          >
          > I'm not sure this will work, but the variable ought to break up the
          > output pattern:
          >
          > ^!replace "\b(\d+)\.?(\d*)\b" >> "00000$1.$2^%empty%0000" rwais
          It does allow the $2 to be substituted but ^%empty% does not appear to
          get translated.
          I get results like this:
          45.6 ==> 0000045.6^%empty%0000
          --
          Regards ... Alec (buralex@gmail & WinLiveMess - alec.m.burgess@skype)
        • Eb
          Ok, try this (hex code x30 for the first zero): $2 x30000 Eb
          Message 4 of 14 , Apr 4, 2011
          • 0 Attachment
            Ok, try this (hex code '\x30' for the first zero):

            $2\x30000

            Eb


            --- In ntb-scripts@yahoogroups.com, Alec Burgess <buralex@...> wrote:
            >
            > > ^!replace "\b(\d+)\.?(\d*)\b" >> "00000$1.$2^%empty%0000" rwais
            > It does allow the $2 to be substituted but ^%empty% does not appear to
            > get translated.
            > I get results like this:
            > 45.6 ==> 0000045.6^%empty%0000
            > --
            > Regards ... Alec (buralex@gmail & WinLiveMess - alec.m.burgess@skype)
            >
          • Alec Burgess
            Thanks Eb - x30 works. when I was messing around with this I had tried the same thing but realize now that I was trying (the meaningless) uppercase X30
            Message 5 of 14 , Apr 4, 2011
            • 0 Attachment
              Thanks Eb - \x30 works.
              when I was messing around with this I had tried the same thing but
              realize now that I was trying (the meaningless) uppercase \X30 instead
              of the correct \x30. Ooops ! :-[

              On 2011-04-04 09:18, Eb wrote:
              > Ok, try this (hex code '\x30' for the first zero):
              >
              > $2\x30000
              >
              > Eb
              >
              > --- In ntb-scripts@yahoogroups.com
              > <mailto:ntb-scripts%40yahoogroups.com>, Alec Burgess <buralex@...> wrote:
              > >
              > > > ^!replace "\b(\d+)\.?(\d*)\b" >> "00000$1.$2^%empty%0000" rwais
              > > It does allow the $2 to be substituted but ^%empty% does not appear to
              > > get translated.
              > > I get results like this:
              > > 45.6 ==> 0000045.6^%empty%0000

              --
              Regards ... Alec (buralex@gmail & WinLiveMess - alec.m.burgess@skype)


              [Non-text portions of this message have been removed]
            Your message has been successfully submitted and would be delivered to recipients shortly.