trim and REReplace | Member | | Join Date: Jan 2008
Posts: 36
| | |
Hi,
I have this number: 00000000010000000099B 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
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: trim and REReplace
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!
| | Member | | Join Date: Jan 2008
Posts: 36
| | | re: trim and REReplace
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?
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: trim and REReplace
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?
| | Member | | Join Date: Jan 2008
Posts: 36
| | | re: trim and REReplace
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.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: trim and REReplace
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.
| | Member | | Join Date: Jan 2008
Posts: 36
| | | re: trim and REReplace
Cool. I'll try it and let you know.
| | Member | | Join Date: Jan 2008
Posts: 36
| | | re: trim and REReplace
Here is the code:
<cfset StreetNumber = REReplace(#StreetNumber#,"^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 00000000010000000099B
| | Member | | Join Date: Jan 2008
Posts: 36
| | | re: trim and REReplace
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,
"00000000010000000099B" 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:-)
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: trim and REReplace
Sorry, $1-$3 was JavaScript (and possibly other languages) notation for back-referencing. In Coldfusion, it's "\1-\3".
| | Member | | Join Date: Jan 2008
Posts: 36
| | | re: trim and REReplace
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.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: trim and REReplace
An easy way would be to use two rereplaces, one for the first and one for the second number, e.g. - <cfset startRange = REReplace(left(StreetNumber,10),"0+","")>
-
<cfset endRange = REReplace(right(StreetNumber,11),"^0+([1-9][0-9]+)[OEB]","\1")>
-
<cfset StreetNumber = startRange & endRange>
| | Member | | Join Date: Jan 2008
Posts: 36
| | | re: trim and REReplace
You’re awesome!!!!!
It's done exactly the way I wanted. Thank you sooooooo much for giving me your time. I really appreciate it.
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: trim and REReplace
No problem, you're welcome as always.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|