question about find and replace

Robert Brenstein rjb at robelko.com
Tue Sep 30 13:07:39 EDT 2008


On 30.09.2008 at 8:54 Uhr +0100 Peter Alcibiades apparently wrote:
>How do you pad out a series of numbers with leading zeros?  Like for instance
>the series is
>
>.1.
>.2.
>.3.
>.11.
>.42.
>.98.
>
>and you want them to be
>
>.001.
>.002.
>.003.
>.011.
>.042.
>.098.
>
>I know how to find them, using the fact that they appear as shown between
>two . characters,  but then I don't know how to use regular expressions to
>put part of what has been found back along with the padding zeros.  Or should
>you not be trying to use regex at all?  I seem to recall something about look
>ahead and greediness but never really understood it, is this relevant?
>
>Peter

Simple brute force method is:

put length(theNumber) into theNumberLength
if theNumberLength < 5 then
    get zero
    if theNumberLength < 4 then get zero & zero
    put it after char 1 of theNumber
end if

same -- a bit less efficient but shorter:

if length(theNumber) < 4 then put zero after char 1 of theNumber
if length(theNumber) < 5 then put zero after char 1 of theNumber

If the numbers are part of a longer string, you may need to use 
offset function to find the insert position.

Robert



More information about the use-livecode mailing list