Removing extra spaces and or hyphens

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm
Location: USA

Removing extra spaces and or hyphens

Post by shawnblc » Tue Sep 01, 2020 4:02 pm

I'm trying to remove all extra spaces and hyphens. In the field there should only be allowed one space between words or one hyphen, but never both and never more than one.

I'm currently doing something like this, more it doesn't catch everything. What's a better way than this elementary way?

Code: Select all

   replace "   " with "-" in gFilename
   replace "  " with "-" in gFilename
   replace " " with "-" in gFilename
   replace "--" with "-" in gFilename
   replace "---" with "-" in gFilename
   replace "----" with "-" in gFilename
   replace "-----" with "-" in gFilename
   replace "------" with "-" in gFilename
   replace "-------" with "-" in gFilename
   replace "--------" with "-" in gFilename
   replace "---------" with "-" in gFilename
   replace "----------" with "-" in gFilename
   replace "-----------" with "-" in gFilename
   replace "------------" with "-" in gFilename
   replace "-------------" with "-" in gFilename
   replace "--------------" with "-" in gFilename
   replace "---------------" with "-" in gFilename
   replace "----------------" with "-" in gFilename

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Removing extra spaces and or hyphens

Post by bn » Tue Sep 01, 2020 4:56 pm

What I do is:

Code: Select all

   put the length of gFileName into tLength
   repeat
      replace "  " with " " in gFileName
      if the length of gFileName = tLength then
         exit repeat
      end if
      put the length  of gFileName into tLength
   end repeat
   
   put the length of gFileName into tLength
   repeat
      replace "--" with "-" in gFileName
      if the length of gFileName = tLength then
         exit repeat
      end if
      put the length  of gFileName into tLength
      
      -- gFileName should be alright now
   end repeat

Kind regards
Bernd

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Removing extra spaces and or hyphens

Post by Thierry » Tue Sep 01, 2020 5:02 pm

bn wrote:
Tue Sep 01, 2020 4:56 pm
What I do is:
What I do is:

Code: Select all

put replaceText( yourInputText, "([ ]+|-+)", "-")
ans slighty faster as it doesn't replace single hyphen:

Code: Select all

put replaceText(someText, "([ ]+|--+)", "-")

As usual, different ways...

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Removing extra spaces and or hyphens

Post by dunbarx » Tue Sep 01, 2020 5:08 pm

Bernd does something like what I did a long while ago. Given a field with excess spaces, and a fld 2 to receive the output:

Code: Select all

on mouseUp
   get fld 1 --field contains all those extra spaces
   repeat until "  " is not in it
      replace "  " with space in it
   end repeat
   put it into fld 2
end mouseUp
Same for any char. In my old handler, I cycled through a list of such recalcitrant chars:

Code: Select all

on mouseUp
   get fld 1 --field contains all those extra spaces
   repeat until "  " is not in it
      replace "  " with space in it
   end repeat
   
   repeat until "--" is not in it
      replace "--" with "-" in it
   end repeat
   
   --etc.
   
   put it into fld 2
end mouseUp
Craig
Last edited by dunbarx on Tue Sep 01, 2020 5:15 pm, edited 2 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9669
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Removing extra spaces and or hyphens

Post by dunbarx » Tue Sep 01, 2020 5:12 pm

By the way, I always trust Thierry's regex expertise. I just rarely understand it.

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4003
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Removing extra spaces and or hyphens

Post by bn » Tue Sep 01, 2020 5:14 pm

Hallo Thierry,

???

using the code snippet of shawn regex gives
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
-replace-"-"-with-"-"-in-gFilename
versus
replace " " with "-" in gFilename
replace " " with "-" in gFilename
replace " " with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
replace "-" with "-" in gFilename
I even upgraded BBEdit to have the regex playground. I am trying to get there...

Kind regards
Bernd

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Removing extra spaces and or hyphens

Post by Thierry » Tue Sep 01, 2020 5:25 pm

bn wrote:
Tue Sep 01, 2020 5:14 pm
Hallo Thierry,

???

I even upgraded BBEdit to have the regex playground. I am trying to get there...
Wow, Bernd jumping in! Congratulations.

Actually, I misread the OP,
so to only replace double or more spaces, change this part:

Code: Select all

[ ]+
with:

Code: Select all

 [ ][ ]+
Better?

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Removing extra spaces and or hyphens

Post by Thierry » Tue Sep 01, 2020 5:27 pm

dunbarx wrote: By the way, I always trust Thierry's regex expertise.
Thank you!
dunbarx wrote: I just rarely understand it.
So do I :)

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

shawnblc
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 342
Joined: Fri Jun 01, 2012 11:11 pm
Location: USA

Re: Removing extra spaces and or hyphens

Post by shawnblc » Tue Sep 01, 2020 5:29 pm

Thanks everyone. Working like a charm.

Thierry wrote:
Tue Sep 01, 2020 5:02 pm
bn wrote:
Tue Sep 01, 2020 4:56 pm
What I do is:
What I do is:

Code: Select all

put replaceText( yourInputText, "([ ]+|-+)", "-")
ans slighty faster as it doesn't replace single hyphen:

Code: Select all

put replaceText(someText, "([ ]+|--+)", "-")

As usual, different ways...

Regards,

Thierry

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”