trim and REReplace

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cfmx2008
    New Member
    • Jan 2008
    • 36

    trim and REReplace

    Hi,

    I have this number: 000000000100000 00099B I need to remove the 0s before 10 and 99 and the alphabets like B after 99. I also need to replace the 0s between 10 and 99 by "-". I don't know how to use trim and Rereplace for these two numbers in this string. Any Idea? I appreciate it. Thanks
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    What's the pattern of these strings?

    Replacing 0 and B is easy, but the 10 would make it more difficult because it has a 0!

    Comment

    • cfmx2008
      New Member
      • Jan 2008
      • 36

      #3
      Agree. I have this problem too. The pattern is : 10 digits at the beginning of the string (for the 1st number) + 10 more digits after (for the 2ed number) + 1 alphabet.

      I think we should clean the 4 or 5 first digits (0s) from the beginning of the string then leave the next 5 digits then insert our "-" and again clean the 4 or 5 next 0s and leave the rest and again clean the alphabet. What do you think?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        That does make the problem easier because you have a consistent pattern.

        However, I notice that in the example string you have 9 0s then 10. Is that correct or should it be 8 0s then a 10?

        Comment

        • cfmx2008
          New Member
          • Jan 2008
          • 36

          #5
          In this example I have 9 0s. but remember the 1st 10 digits are belong to 1st number. In this case number is 1 so we have 9 0s left. The real number could be 111 then we will have 7 0s before the number.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Use something like ^0+([1-9]+)(0+)([1-9]+)([A-Z]?)$ and replace with $1-$3.

            I haven't tested this with REReplace, but it should give you an idea. It matches 0s at the beginning followed by a number followed by 0s followed by a number followed by letters.

            Comment

            • cfmx2008
              New Member
              • Jan 2008
              • 36

              #7
              Cool. I'll try it and let you know.

              Comment

              • cfmx2008
                New Member
                • Jan 2008
                • 36

                #8
                Here is the code:
                <cfset StreetNumber = REReplace(#Stre etNumber#,"^0+([1-9]+)(0+)([1-9]+)([A-Z]?)$","$1-$3", "all")>

                it will replace the whole StreetNumber by $1-$3. I tried it with trim() as well. It does the same. #StreetNumber# is 000000000100000 00099B

                Comment

                • cfmx2008
                  New Member
                  • Jan 2008
                  • 36

                  #9
                  I Got the exact pattern:

                  Street number ranges for near matches are returned in the following fixed-width format:

                  START RANGE (10 bytes) + END RANGE (10 bytes) + OEB code (1 byte) left-padded with zeros



                  Secondary (apartment) ranges for near matches are returned in the following fixed-width format:

                  START RANGE (8 bytes) + END RANGE (8 bytes) + OEB code (1 byte) left-padded with zeros



                  The OEB is “odd-even-both” code, meaning that the range either contains all Odd numbers, Even numbers, or Both.



                  In our example,

                  "00000000010000 000099B" means “buildings 1-99 Both odd and even”, i.e. {1,2,3,4,…. 98,99}



                  As you see, all we need to do is chop off all leading zeros in each of the range blocks.

                  Please don't give up on me:-)

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Sorry, $1-$3 was JavaScript (and possibly other languages) notation for back-referencing. In Coldfusion, it's "\1-\3".

                    Comment

                    • cfmx2008
                      New Member
                      • Jan 2008
                      • 36

                      #11
                      Thanks. But this replacement works only for this example. Did you get the chance to see the exact pattern I've posted? I need the "-" be in the middle of the numbers after cleaning. ^0+ works for START RANGE (10 bytes) But not for the END RANGE (10 bytes) because it will remove the 0s even if the start number is 10 or 100.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        An easy way would be to use two rereplaces, one for the first and one for the second number, e.g.
                        [code=cfm]<cfset startRange = REReplace(left( StreetNumber,10 ),"0+","")>
                        <cfset endRange = REReplace(right (StreetNumber,1 1),"^0+([1-9][0-9]+)[OEB]","\1")>
                        <cfset StreetNumber = startRange & endRange>[/code]

                        Comment

                        • cfmx2008
                          New Member
                          • Jan 2008
                          • 36

                          #13
                          You’re awesome!!!!!
                          It's done exactly the way I wanted. Thank you sooooooo much for giving me your time. I really appreciate it.

                          Comment

                          • acoder
                            Recognized Expert MVP
                            • Nov 2006
                            • 16032

                            #14
                            No problem, you're welcome as always.

                            Comment

                            Working...