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

17786Re: Dollars and cents

Expand Messages
  • dracorat
    Apr 25, 2008
    • 0 Attachment
      You bet!

      In the find expression (what's left of >>)

      \b means "boundary" and matches the outer edges of any alpha numeric
      sequence. It is close (but not exactly like) to the equation

      (If your regular expression understanding is not yet up to
      understanding that, just remember, edges of words)

      So, by wrapping a regular expression in \b on both sides we force
      whole word matches (or in this case, whole number sequences.)

      So, starting the explanation again:
      \b - beginning of word
      ( - What's about to come, take note of
      \d - a single decimal character
      ) - stop taking note - from ( to ) we have our first "match" which in
      the output gets assigned to the variable $1
      c - this is a literal character because it doesn't have a backslash
      with it. So, we are telling it to find the letter "c". If we had the
      "c" in the parenthesis it would be a part of the $1 variable, which
      means it would be copied to the output and we don't want that. That's
      why it's outside the parenthesis.
      \b - end of word (so we don't match something like 2c2 for example. Or
      2cell or anything else that's not one digit and a letter "c".)

      Then, in the output (the area to the right of >>) we have to tell it
      what to do with what we found. Remember we saved the number as $1.

      \$ - because the dollar sign is usually special to regular
      expressions, we have to tell it that we really want a dollar sign. So
      it has a backslash before it which means "this really is a dollar sign
      that I want"
      0.0 - because we matched one digit numbers only, we are putting in the
      dollars as 0, a decimal point, and the dimes as zero.
      $1 - we captured one digit and it goes here. Remember, $1 is our variable.

      Thus, let's say the number was 6c

      We captured the 6. The "c" gets discarded because it was a part of the
      regular expression find but was outside of parenthesis.

      Then we inserted "$0.0" and then "6" which becomes "$0.06"

      Now, there's no way to tell regular expressions to formulate numbers
      with leading zeroes, which would be convenient, so we have to write
      regular expressions for each case where there would be a leading zero.

      So, the second expression is the same with two changes.

      First, it matches two digits, not one. (That's the \d\d sequence.)

      Since we are finding two digits, it doesn't make sense to count zero
      dimes and then insert a two digit number, so the output only specifies
      the whole dollar portion (which is 0).

      For the record, \d\d is functionally equivalent to \d{2} but when
      matching two to three characters I tend to just specify it like I did.
      I think it makes it more readable. But neither is more or less correct.

      Now, I could have ended there and fully answered your request, but I
      felt that in the event you had a three digit cents specification
      somewhere that you would want it changed (not likely, but possible) I
      finished with the last one too. What it does is find anything that is
      three or greater digits. It saves the last two digits to $2 and
      everything before that to $1.


      Word boundary, Any number of digits (save to $1), two digits (save to
      $2), the letter "c", word boundary.

      Then, in the output, since now we have a count of the number of
      dollars (in the sequence 1234c that would be 12 dollars) we don't
      specify 0 but rather what we discovered.

      And there you have it!


      (Admins, am I allowed to make a product recommendation that's a cheat
      sheet for regular expressions?)

      --- In ntb-clips@yahoogroups.com, "ehofferino" <eyhoffmann@...> wrote:
      > Thanks Keith!
      > I would love an explanation. Teach a man to fish...
      > --- In ntb-clips@yahoogroups.com, "dracorat" <dracorat@> wrote:
      > >
      > > In a new clip, use:
      > >
      > > ^!REPLACE "\b(\d)c\b" >> "\$0.0$1" AWRS
      > > ^!REPLACE "\b(\d\d)c\b" >> "\$0.$1" AWRS
      > > ^!REPLACE "\b(\d+)(\d\d)c\b" >> "\$$1.$2" AWRS
      > >
      > > If you want me to explain the regular expressions, just say the word.
      > > --Keith
      > >
      > > --- In ntb-clips@yahoogroups.com, "ehofferino" <eyhoffmann@> wrote:
      > > >
      > > > Hi guys,
      > > >
      > > > I'm looking to write a simple clip that searches through a block of
      > > > text and replaces all instances of monetary notation that look like
      > > this:
      > > >
      > > > 44c
      > > >
      > > > with this
      > > >
      > > > $0.44.
      > > >
      > > > Here's what the block might look like before changes:
    • Show all 7 messages in this topic