| View previous topic :: View next topic |
| Author |
Message |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Tue Feb 26, 2008 12:35 pm Post subject: Removing Redundant Spaces / Code required for RegExReplace |
|
|
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.  |
|
| Back to top |
|
 |
tic
Joined: 22 Apr 2007 Posts: 1356
|
Posted: Tue Feb 26, 2008 12:55 pm Post subject: |
|
|
| 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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Tue Feb 26, 2008 1:03 pm Post subject: |
|
|
Many thanks tic . I am nesting two calls, it would be nice if the expressions could be merged,
| Code: | Str := " The Quick Brown Fox Jumps Over the Lazy Dog "
NewStr := "[" RegExReplace(RegExReplace(Str, " +", " "),"(^\s*|\s*$)") "]"
MsgBox, % newStr |
 |
|
| Back to top |
|
 |
haichen
Joined: 05 Feb 2007 Posts: 110 Location: Osnabrück, Germany
|
Posted: Tue Feb 26, 2008 1:35 pm Post subject: |
|
|
It is not what you want. But I like this(a bit crazy):
| Code: | delspaces(str){
StringSplit,MyArray,str,%a_space%,%a_space%
loop,%MyArray0%
{
x:=MyArray%a_index%
strg =%strg% %x%
}
return %strg%
} |
|
|
| Back to top |
|
 |
tic
Joined: 22 Apr 2007 Posts: 1356
|
Posted: Tue Feb 26, 2008 2:07 pm Post subject: |
|
|
| 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 |
|
 |
haichen
Joined: 05 Feb 2007 Posts: 110 Location: Osnabrück, Germany
|
Posted: Tue Feb 26, 2008 2:22 pm Post subject: |
|
|
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.
 |
|
| Back to top |
|
 |
haichen
Joined: 05 Feb 2007 Posts: 110 Location: Osnabrück, Germany
|
Posted: Tue Feb 26, 2008 2:38 pm Post subject: |
|
|
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 |
|
 |
haichen
Joined: 05 Feb 2007 Posts: 110 Location: Osnabrück, Germany
|
Posted: Tue Feb 26, 2008 3:33 pm Post subject: |
|
|
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 |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 901
|
Posted: Tue Feb 26, 2008 7:42 pm Post subject: |
|
|
| Code: | Str := " The Quick Brown Fox Jumps Over the Lazy Dog "
NewStr := "[" RegExReplace(Str, "(^\s+| +(?= )|\s+$)") "]"
MsgBox, % newStr |
|
|
| Back to top |
|
 |
haichen
Joined: 05 Feb 2007 Posts: 110 Location: Osnabrück, Germany
|
Posted: Tue Feb 26, 2008 8:13 pm Post subject: |
|
|
| This don't work with tabs. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Tue Feb 26, 2008 8:21 pm Post subject: |
|
|
Many thanks Manauser. Great help.
| 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.  |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Tue Feb 26, 2008 11:15 pm Post subject: |
|
|
| If you like math, read this. There is no need for loops, or RegEx... |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
|
| Back to top |
|
 |
haichen
Joined: 05 Feb 2007 Posts: 110 Location: Osnabrück, Germany
|
Posted: Wed Feb 27, 2008 12:57 am Post subject: |
|
|
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 |
|
 |
Oberon
Joined: 18 Feb 2008 Posts: 458
|
Posted: Wed Feb 27, 2008 1:06 am Post subject: |
|
|
| 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 |
|
 |
|