regEx to remove spaces ?

Jim Ault JimAultWins at yahoo.com
Wed May 17 14:16:37 EDT 2006


On 5/17/06 2:45 AM, "jbv" <jbv.silences at club-internet.fr> wrote:

> Is it possible to use regEx to remove spaces before and
> after quotes, and, if yes, how ?
> Example :
>     my " beautiful " laundrette
> becomes
>     my "beautiful" laundrette

There are a few ways to use RegEx, depending on the exact situations you
face.  Your question targets two strings that are identical, occuring at
opposite ends of a word or phrase.

[A Rev solution follows at the bottom.]

So
will there be more than one quoted passage per line
will there be a return in the middle of the quote (or more than one cr)
will the white space next to the quote always be a space
   eg ( " beautiful " launderette,  but knobs are " chinsy ". )
thus a period follows the last quote, not a space.

RegEx has simple pattern replacement, plus and/or logic, plus lookForward,
lookBackwards.  Thus it has very sophisticated algorithms possible, so it is
important that you be very specific about the task.

So, are all the possible strings (with commas and periods)?
s"sWORDs"s
s"sWORDs",
s"sWORDs".
,"sWORDs"s
s" WORDs"s
s"sWORDs"s

which means we are looking for 4 strings
(s"s)(,"s)(s".)(s",)
on the same line defined by CR (or LFCR which is a Windows end of line
marker?)

In RegEx, one expression can contain the (this|that|other) format, allowing
alternate hits to be considered.  Also, RegEx can find the first occurance,
then scan to find the next either forward to the end, or back toward the
front of the string.  As you see, it can get more complicated than you ever
wanted.

One Rev technique that could get you where you want to go is...
--handles multiple instances, spanning across lines
--but this ver does not look for trailing comma or period.

--text in------------------------------------------
The " dream " continues
and will not " die ", even a " slow,
mangled " death
and lives for the ages.
IBID 26
-------------------------------------
on mouseDoubleUp
  put fld 1 into txt
  replace cr with "MMMM" in txt  --protect line wraps
  replace (" "&quote&" ") with (" "&cr&" ") in txt
  repeat with x = the number of lines in txt down to 2
    if char 1  of line x of txt is space and char 1 of line x-1 of txt is
space then
      put word 1 to -1 of line x-1 of txt into line x-1 of txt
    end if
  end repeat
  replace cr with quote in txt
  replace "MMMM" with cr in txt
  put txt into fld 2
end mouseDoubleUp
--text out-------------------------------------------------
The "dream" continues
and will not "die" even a "slow,
mangled" death
and lives for the ages.
IBID 26
------------------------------------------------------

More than you had imagined, I am sure.

Jim Ault
Las Vegas






More information about the Use-livecode mailing list