regEx to remove spaces ?

Martin Baxter mb.ur at harbourhosting.co.uk
Fri May 19 16:56:17 EDT 2006


# How about:

put "my " & quote & " beautiful " & quote & " laundrette" into srctext
put "<" into repchar
put 0 into oldoff
put 1 into toff
repeat while toff > 0
  put offset(quote,srctext,oldoff) into toff
  if toff > 0 then
  put repchar into char oldoff + toff of srctext
  add toff to oldoff
   if repchar ="<" then
    put ">" into repchar
   else
    put "<" into repchar
   end if
  end if
end repeat
put replacetext(srctext,"<[ ]+",quote) into srctext
put replacetext(srctext,"[ ]+>",quote) into srctext
put srctext

-- output: my "beautiful" laundrette

# Not a very pretty or robust script but it worked on your test data.

I don't know if there will only ever be one consecutive superfluous 
space but I had it clean up multiple spaces anyway.

There is a big assumption in this that opening and closing quotes are 
always balanced. If this is not the case, this procedure will give bad 
results.

Also if the characters "<" and ">" might occur in the data, different 
substitution characters should be used. These are OK on this example.

Martin Baxter

jbv wrote:
> Hi,
> 
> thanks for the response, but I don't think it would work...
> 
> let's go back to my example :
>     my " beautiful " laundrette
> 
> if you replace tOpenSpace first, then you get :
>     my "beautiful "laundrette
> 
> and then if you replace tCloseSpace, you get
>     my"beautiful"laundrette
> 
> Thanks anyway,
> JB
> 
>>
>> I'm curious what mandates regEx?
>>
>> if the means is not an issue in reaching the goal ,  doesn't
>> "replace" work for you?
>>
>> This is obvious, but for the record:
>>
>> function wipeQuotedSpaces tText
>>         put (quote & " ") into tOpenSpace
>>         put ( " " & quote) into tCloseSpace
>>         replace tOpenSpace with quote in tText
>>         replace tCloseSpace with quote in tText
>>         return tText
>> end wipeQuotedSpaces
>>
>>> Thanks,
>>> JB
> 





More information about the use-livecode mailing list