Page 1 of 2

Replace text of any characters between parentheses with a space

Posted: Thu Jun 10, 2021 9:09 am
by richmond62
This is carried over from the Use-List . . .
I want to replace all text within in parentheses (including the parentheses) with a single space. I have tried assembling the matchExpression in advance and in line, and also escaping the parentheses just in case they are reserved in regex. Or do I need to enclose the matchExpression in something other than or in addition to, quotes?
I did things another way:

Code: Select all

on mouseUp
   put empty into fld "f2"
   put fld "fff" into FLUFFY
   put 0 into CHEK
   repeat until FLUFFY is empty
      put char 1 of FLUFFY into FUR
      delete char 1 of FLUFFY
      switch FUR
         case "("
            put 1 into CHEK
            break
         case ")"
            put 0 into CHEK
            break
         default
            if CHEK < 1 then 
               put FUR after fld "f2"
            else
               --- do nix
            end if
      end switch
   end repeat
end mouseUp
-
Screen Shot 2021-06-10 at 11.07.40 AM.png

Re: Replace text of any characters between parentheses with a space

Posted: Thu Jun 10, 2021 9:10 am
by richmond62
Um: forgot to put a SPACE in, just deleted everything . . .

Re: Replace text of any characters between parentheses with a space

Posted: Fri Jun 11, 2021 5:52 pm
by jsburnett
I tried replaceText but could not get that to work.
Try this.


"on mouseUp
put field 1 into tText
repeat with x = the number of chars of tText down to 0
if char x of tText is "(" or char x of tText is ")"
then
delete char x of tText
end if
end repeat
put tText
end mouseUp:

Field 1 is the starting text.

John Burnett

Re: Replace text of any characters between parentheses with a space

Posted: Fri Jun 11, 2021 6:09 pm
by jmburnod
Hi Richmond,
Thanks for sharing]
DeParenthesizer works fine here
Best
Jean-Marc

Re: Replace text of any characters between parentheses with a space

Posted: Fri Jun 11, 2021 9:47 pm
by bogs
Um, jsburnett, that will certainly take out parenthesis, but that was only part of the equation posted.
first post wrote: I want to replace all text within in parentheses (including the parentheses) with a single space.
I'm pretty sure you could do this easily with offset (character level to find the parenthesis and set up a marker for each) and replaceText (marker 1 to marker 2) in a fairly small repeat loop.

Re: Replace text of any characters between parentheses with a space

Posted: Fri Jun 11, 2021 10:12 pm
by jsburnett
O. Oops

How about “offset”.
Use offset to find the “(“ the. “)” chars.
Then delete the chars in between.

Repeat this till no pairs remain.

Just an idea off the top of my head.

Or use the other script and keep deleting till the “)” is ‘found’

Re: Replace text of any characters between parentheses with a space

Posted: Fri Jun 11, 2021 11:17 pm
by bogs
jsburnett wrote:
Fri Jun 11, 2021 10:12 pm
How about “offset”.
Use offset to find the “(“ the. “)” chars.
Then delete the chars in between.
Hee hee, I thought I said that :P

This also apparently works ok...

Code: Select all

on mouseUp
   put "" into field 2
   put field 1 into theSource
   
   repeat with x=1 to the number of characters of theSource
      wait 5 ticks with messages
      if character x of theSource is not "(" then 
         put character x of theSource after field 2
      else
         put offset(")",theSource) into blipMe
         replace character x to blipMe of theSource with " " in theSource
         next repeat
      end if
   end repeat
end mouseUp
Image

Re: Replace text of any characters between parentheses with a space

Posted: Fri Jun 11, 2021 11:49 pm
by jsburnett
Ok, I may not be seeing the entire thread.
out

Re: Replace text of any characters between parentheses with a space

Posted: Sat Jun 12, 2021 8:22 am
by AxWald
Hi,

Code: Select all

on mouseUp
   get fld "f1_fld"
   put false into mySkip
   repeat for each char C in it
      if C = "(" then
         put true into mySkip
         put space after myRes   --  the "single space"
      else if C = ")" then
         put false into mySkip
      else if not mySkip then
         put C after myRes
      end if
   end repeat
   replace "   " with " " in myRes  --  optional, restores it to a nice text
   put myRes into fld "f2_fld"
end mouseUp
;-)

Have fun!

Re: Replace text of any characters between parentheses with a space

Posted: Sat Jun 12, 2021 6:21 pm
by dunbarx
So why does this not work:

Code: Select all

filter textWithParens without "(*)"
Craig

Re: Replace text of any characters between parentheses with a space

Posted: Sat Jun 12, 2021 6:36 pm
by stam
Or just use regex.

There was a discussion. It so long ago here:
viewtopic.php?f=7&t=35679

The discussion was on how to find text between 2 delimiters. Regex works very well for this and is less code…

Re: Replace text of any characters between parentheses with a space

Posted: Sat Jun 12, 2021 7:19 pm
by richmond62
Why does regex get on my nerves?

Possibly because I have a "thing" about doing everything inwith LiveCode as such. :shock:

Or, just possibly because it does NOT look like LiveCode, but much more like the type of computer languages I almost
completely turned my back on in 1993 (When I discovered HyperCard):
-
SShot 2021-06-12 at 21.17.26.png

Re: Replace text of any characters between parentheses with a space

Posted: Sat Jun 12, 2021 9:00 pm
by jacque

Code: Select all

put replacetext(fld 1,"\(.*?\)",space) into tNewText

Re: Replace text of any characters between parentheses with a space

Posted: Sat Jun 12, 2021 9:25 pm
by bogs
I dunno why regex gets on your nerves Richmond, it is no more or less than a few plain english symbols and the command as Jacque put it is certainly shorter than anything else that popped in so far. Orders of magnitude faster as well I wager.

I keep meaning to get around to parsing it, but usually in the time it would take me to doing that, I've already figured out a (short enough) brute force method :roll:

Re: Replace text of any characters between parentheses with a space

Posted: Sat Jun 12, 2021 9:26 pm
by richmond62
Replace text of any characters between parentheses with a space
OK: pause for 'evil' Richmond question:

Is that to be interpreted that EACH character between parentheses MUST be REPLACED with a SPACE?
(= as many SPACES as their are characters between parentheses.)

Or

that ALL characters between parentheses MUST be REPLACED with a SPACE?
(=1 SPACE regardless of length of string between parentheses.)