Page 1 of 1
Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 4:02 pm
by shawnblc
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
Re: Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 4:56 pm
by bn
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
Re: Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 5:02 pm
by Thierry
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
Re: Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 5:08 pm
by dunbarx
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
Re: Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 5:12 pm
by dunbarx
By the way, I always trust Thierry's regex expertise. I just rarely understand it.
Craig
Re: Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 5:14 pm
by bn
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
Re: Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 5:25 pm
by Thierry
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:
with:
Better?
Thierry
Re: Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 5:27 pm
by Thierry
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
Re: Removing extra spaces and or hyphens
Posted: Tue Sep 01, 2020 5:29 pm
by shawnblc
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