AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Removing Redundant Spaces / Code required for RegExReplace
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
SKAN



Joined: 26 Dec 2005
Posts: 5890

PostPosted: Tue Feb 26, 2008 12:35 pm    Post subject: Removing Redundant Spaces / Code required for RegExReplace Reply with quote

I need help in removing spaces from the following String
Code:
Str := "    The   Quick Brown    Fox Jumps  Over the  Lazy    Dog   "

I already have regex to remove the leading and trailing spaces but I also want to remove the redundant spaces found within the string.
I remember seeing it somewhere in the forum. Please provide me the RegEx or a link

Thanks. Smile
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1356

PostPosted: Tue Feb 26, 2008 12:55 pm    Post subject: Reply with quote

Code:
Str := "    The   Quick Brown    Fox Jumps  Over the  Lazy    Dog   "
NewStr := RegExReplace(Str, " +", " ")
If (Substr(Str, 1, 1) = " ")
StringTrimLeft, NewStr, NewStr, 1
If (Substr(Str, 0) = " ")
StringTrimRight, NewStr, NewStr, 1
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5890

PostPosted: Tue Feb 26, 2008 1:03 pm    Post subject: Reply with quote

Many thanks tic Smile. I am nesting two calls, it would be nice if the expressions could be merged, Rolling Eyes

Code:
Str := "    The   Quick Brown    Fox Jumps  Over the  Lazy    Dog   "
NewStr := "[" RegExReplace(RegExReplace(Str, " +", " "),"(^\s*|\s*$)") "]"
MsgBox, % newStr


Smile
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 110
Location: Osnabrück, Germany

PostPosted: Tue Feb 26, 2008 1:35 pm    Post subject: Reply with quote

It is not what you want. But I like this(a bit crazy): Wink

Code:
delspaces(str){
   StringSplit,MyArray,str,%a_space%,%a_space%
   loop,%MyArray0%
   {
      x:=MyArray%a_index%
      strg =%strg% %x%
   }
   return %strg%
}
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1356

PostPosted: Tue Feb 26, 2008 2:07 pm    Post subject: Reply with quote

haichen, that achieves the same goal, but 16 times slower. you should always avoid loops in ahk if not needed. it is a scripting (parsing) language, so loops slow it down.
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 110
Location: Osnabrück, Germany

PostPosted: Tue Feb 26, 2008 2:22 pm    Post subject: Reply with quote

Thanks for make clear that it is slow.
I know that, but I like this because it is easy to understand, in contradiction
to Regular Expressions.
If it is easier to understand, you can avoid mistakes.
Very Happy
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 110
Location: Osnabrück, Germany

PostPosted: Tue Feb 26, 2008 2:38 pm    Post subject: Reply with quote

What about this?
only spaces
Code:
Str := "    The   Quick Brown    Fox Jumps  Over the  Lazy    Dog   "
NewStr := "[" RegExReplace(Str,"\x20{2,}", " ") "]"
MsgBox, % newStr


also tabs:
Code:
Str := "    The   Quick Brown    Fox Jumps  Over the  Lazy    Dog   "
NewStr := "[" RegExReplace(Str,"\s{2,}", " ") "]"
MsgBox, % newStr
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 110
Location: Osnabrück, Germany

PostPosted: Tue Feb 26, 2008 3:33 pm    Post subject: Reply with quote

This deletes all redundant spaces in the string and deletes all spaces around the string.
Also when AutoTrim is Off.

Code:
AutoTrim, Off
Str := "    The   Quick Brown   Fox Jumps  Over the  Lazy    Dog  "
str:=delspaces(str)
msgbox,[%Str%]

delspaces(str){
str :=RegExReplace(Str,"\s{2,}", " ")         ;whitespace in the string
str :=RegExReplace(Str,"(^\s)|(\s$)","")      ;trailing whitespace
return %str%
}
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 901

PostPosted: Tue Feb 26, 2008 7:42 pm    Post subject: Reply with quote

Code:
Str := "    The   Quick Brown    Fox Jumps  Over the  Lazy    Dog   "
NewStr := "[" RegExReplace(Str, "(^\s+| +(?= )|\s+$)") "]"
MsgBox, % newStr
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 110
Location: Osnabrück, Germany

PostPosted: Tue Feb 26, 2008 8:13 pm    Post subject: Reply with quote

This don't work with tabs.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5890

PostPosted: Tue Feb 26, 2008 8:21 pm    Post subject: Reply with quote

Many thanks Manauser. Great help. Smile

Quote:
This don't work with tabs.


Luckily, I am building the string and I am sure it will contain no tabs.
It costs too much code to build the string with proper spaces, in the first place. So I construct it with extra spaces and patch it up with the RegExReplace().

Thanks haichen. Smile
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4016
Location: Pittsburgh

PostPosted: Tue Feb 26, 2008 11:15 pm    Post subject: Reply with quote

If you like math, read this. There is no need for loops, or RegEx...
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5890

PostPosted: Tue Feb 26, 2008 11:26 pm    Post subject: Reply with quote

I have read it already, Sir.
I wanted a solution for my 45 liner Crazy Scripting : NTOW-INR 45L [ Number to Words Expander ] and so opted for RegExReplace() to save a couple of lines.

Thanks Smile
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 110
Location: Osnabrück, Germany

PostPosted: Wed Feb 27, 2008 12:57 am    Post subject: Reply with quote

I think this is the oneliner for tabs and spaces:
It deletes redundant whitespace and trims beginning and end.
One problem remains. It is not sure what kind of whitespace will keep.

Code:
str :=RegExReplace(Str,"^\s+|\s+(?=\s)|\s$")


Nice math from Laszlo. Wasn't there something else about heritage? And with acquaintanceship? I know a person who knows somebody who knows...?
Are our Grand-grand-grand-grand-grandparents cognate?
who knows..
Back to top
View user's profile Send private message
Oberon



Joined: 18 Feb 2008
Posts: 458

PostPosted: Wed Feb 27, 2008 1:06 am    Post subject: Reply with quote

haichen wrote:
One problem remains. It is not sure what kind of whitespace will keep.


Code:
str := "   this  `tonly`t`t`tremoves   `t  same types           of`tspaces   "
MsgBox, % "(" . RegExReplace(str, "(?<=(\s))\1+") . ")"
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group