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

492Trying to perfect RegExp to match various numbers

Expand Messages
  • mycroftj
    Mar 28, 2011
      My ultimate goal was to create a script that (left) space or zero pads numbers to a fixed length for sorting.

      Actual data looks something like

      File 67817 Id.ppt
      File 691037 20dat.sys
      File 69870 Lock.doc
      File 705 56968.mbs
      File 70537 Jil.xls
      File 71168 Gas.jpg


      I then became interested in trying to find a regexp that will match numbers surrounded by BOL, EOL, spaces and tabs with
      signs, decimal point and commas optional.

      In the following test data. the numbers starting with 123 should be matched as well as the integers 0, 1 and 2.
      xxx456 and 456xx should NOT be matched.

      I have something that mostly works but it also matches x456 and the t2 in hmt2. WHY IS THAT?
      It also misses 0, 1 and 2 although it does (correctly) pick up -2.

      I put in the caret because it was not matching numbers at the start of a line. Is that how it's done?

      Thanks for your help. I ordered the Regular Expressions Cookbook today. Hope it's as good as the reviews say!

      Joy

      What I have so far [^\s][\-\+]?\d+,*\d*\.?\d*(?=\s)


      12345
      12345.678
      1234567.90
      xxx456
      456xx

      xx456,123.34
      www.45.67.hmt2

      12,345
      -12,345
      12,345.
      +12,345.01

      12345
      +123,45.678
      1234567.90

      there are 0 lines
      1 or 2 more.

      12345
      -12345.678
      1234567.90
      xxx456
      +456xx

      xx456.34
      www.45.67.hmt2

      +12345

      12345
      -12345.678
      1234567.90
      xxx456
      456xx

      there are 0 lines. The zero should match as should the following one and negative two.
      1 or -2 more.
    • Show all 14 messages in this topic