![Photo](https://secure.gravatar.com/avatar/29daf7e795b7ee51a16a7345151ba65e?s=100&d=%2F%2Fwww.autohotkey.com%2Fboard%2Fpublic%2Fstyle_images%2Fortem%2Fprofile%2Fdefault_large.png)
Put here requests of problems with regular expressions
Started by
PhiLho
, Oct 22 2006 02:18 PM
1074 replies to this topic
The solutions will be posted here, and if of generic interest, collected in another topic which you should check to see if your problem isn't already solved. Check out also the Regular Expression Library for a comprehensive list of such expressions.
#1
-
Posted 22 October 2006 - 02:18 PM
![Put here requests of problems with regular expressions: post #1](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
![Posted Image](http://autohotkey.net/~PhiLho/KM64.png)
How to remove a character that should not exist in beginning/end of string?
Here is a classic example:
In the above example I know that the linefeed will definitely occur as the first character in the built-up string, thus StringTrimLeft is a solution.
I need a one line solution of RegExReplace that would check the Beginning/End of string for a match character and remove it if present.
Something that might take many lines as in TrimPipe() :
I require this for string validation in my CreateArray() function.
Solution:
Thanks to Titan & PhiLho
Here is a classic example:
Loop %A_Windir%\*.bmp BMPFiles = %BMPFiles%`n%A_LoopFileName% StringTrimLeft, BMPFiles, BMPFiles, 1 MsgBox, % BMPFiles
In the above example I know that the linefeed will definitely occur as the first character in the built-up string, thus StringTrimLeft is a solution.
I need a one line solution of RegExReplace that would check the Beginning/End of string for a match character and remove it if present.
Something that might take many lines as in TrimPipe() :
MsgBox % TrimPipe("Apples|Bananas|Cherries") MsgBox % TrimPipe("|Apples|Bananas|Cherries") MsgBox % TrimPipe("Apples|Bananas|Cherries|") MsgBox % TrimPipe("|Apples|Bananas|Cherries|") TrimPipe(Str) { StringLeft, L, Str, 1 IfEqual,L,|, StringTrimLeft, Str,Str,1 StringRight, R, Str, 1 IfEqual,R,|, StringTrimRight, Str,Str,1 Return Str }
I require this for string validation in my CreateArray() function.
Solution:
String1 := "Apples|Bananas|Cherries" String2 := "|Apples|Bananas|Cherries" String3 := "Apples|Bananas|Cherries|" String4 := "|Apples|Bananas|Cherries|" Loop 4, MsgBox % RegExReplace( String%A_Index%, "^\|?(.*?)\|?$", "$1")
Thanks to Titan & PhiLho
![:)](http://www.autohotkey.com/board/public/style_emoticons/default/happy.png)
#2
-
Posted 22 October 2006 - 05:21 PM
![Put here requests of problems with regular expressions: post #2](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
The pipe char needs to be escaped so: MsgBox % RegExReplace("|Apples|Bananas|Cherries|", "^\|?(.*)\|+$", "$1")I need a one line solution of RegExReplace that would check the Beginning/End of string for a match character and remove it if present.
Try removing the first and/or last pipe chars, it still works.
PhiLho, I apologize if you wanted to be the only supporter here.
#3
-
Posted 22 October 2006 - 05:34 PM
![Put here requests of problems with regular expressions: post #3](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
autohotkey.com/net Site Manager
Contact me by email (polyethene at autohotkey.net) or message tidbit
Dear Titan, ![:)](//www.autohotkey.com/board/public/style_emoticons/default/happy.png)
The third one fails .. Should be a small tweak, which I am not able to guess.
Regards,
![:)](http://www.autohotkey.com/board/public/style_emoticons/default/happy.png)
The third one fails .. Should be a small tweak, which I am not able to guess.
MsgBox % RegExReplace("|Apples|Bananas|Cherries|", "^\|?(.*)\|+$", "$1") MsgBox % RegExReplace("Apples|Bananas|Cherries|" , "^\|?(.*)\|+$", "$1") [color=red]MsgBox % RegExReplace("|Apples|Bananas|Cherries" , "^\|?(.*)\|+$", "$1")[/color] MsgBox % RegExReplace("Apples|Bananas|Cherries" , "^\|?(.*)\|+$", "$1")
Regards,
![:)](http://www.autohotkey.com/board/public/style_emoticons/default/happy.png)
#4
-
Posted 22 October 2006 - 06:08 PM
![Put here requests of problems with regular expressions: post #4](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
Thanks Titan, it works! ![:D](//www.autohotkey.com/board/public/style_emoticons/default/grin.png)
How do I use a `n ( linefeed) instead of a pipe? :roll:
![:D](http://www.autohotkey.com/board/public/style_emoticons/default/grin.png)
How do I use a `n ( linefeed) instead of a pipe? :roll:
#6
-
Posted 22 October 2006 - 06:50 PM
![Put here requests of problems with regular expressions: post #6](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
The linefeed is also a control character so you just need to replace \| with \n and use the 'm' (multiline) option: MsgBox, % RegExReplace("`nApples`nBananas`nCherries`n`n", "^\n*(.*[^\n])\n*$", "$1", "m")How do I use a `n ( linefeed) instead of a pipe?
#7
-
Posted 22 October 2006 - 07:10 PM
![Put here requests of problems with regular expressions: post #7](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
autohotkey.com/net Site Manager
Contact me by email (polyethene at autohotkey.net) or message tidbit
The linefeed is also a control character so you just need to replace \| with \n and use the 'm' (multiline) option
I am learning! Thanks..
![:D](http://www.autohotkey.com/board/public/style_emoticons/default/grin.png)
#8
-
Posted 22 October 2006 - 07:19 PM
![Put here requests of problems with regular expressions: post #8](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
regular-expressions.info is a great place to start.
Thanks for the link. I have been peeking into every link that I find pointing to RegEx tutorials. But, I am one of those commoners who find RegEx encryptic.
Learning by examples: IMHO, Dozens of examples are required before a commoner can take a shot at the tutorial. Examples will also help one to understand how RegEx can simplify string manipulation tasks that might otherwise consume many lines.
If I want to learn RegEx without examples I could do it! But that is not the case with everyone. I highly appreciate PhiLho for opening such a topic.
I wish that this topic grows and gets rich with information that anybody can just copy/paste readymade solutions! They will have the apt to learn once they start using RegEx!
#10
-
Posted 22 October 2006 - 08:06 PM
![Put here requests of problems with regular expressions: post #10](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
Certainly not! I am not the only one knowing REs, fortunately, so any contribution is welcome!PhiLho, I apologize if you wanted to be the only supporter here.
#11
-
Posted 23 October 2006 - 08:29 AM
![Put here requests of problems with regular expressions: post #11](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
![Posted Image](http://autohotkey.net/~PhiLho/KM64.png)
How to remove leading spaces, trailing spaces, or both from a string?
With AutoTrim, On ( which is the default setting ),
Variable = %Variable% will remove both the leading spaces as well as the trailing spaces.
I need three seperate one-line RegExReplace solutions that shall
AllTrim : Remove leading spaces, trailing spaces
LTrim: Remove only leading spaces
RTrim: Remove only trailing spaces
:?:
Solution:
Thanks to PhiLho & majkinetor
With AutoTrim, On ( which is the default setting ),
Variable = %Variable% will remove both the leading spaces as well as the trailing spaces.
I need three seperate one-line RegExReplace solutions that shall
AllTrim : Remove leading spaces, trailing spaces
LTrim: Remove only leading spaces
RTrim: Remove only trailing spaces
:?:
Solution:
String := " The Quick Brown Fox " StringA := RegExReplace(String, "(^\s*|\s*$)") MsgBox,0, AllTrim, % "[" StringA "]" StringL := RegExReplace(String, "^\s*") MsgBox,0, LTrim, % "[" StringL "]" StringR := RegExReplace(String, "\s*$") MsgBox,0, RTrim, % "[" StringR "]"
Thanks to PhiLho & majkinetor
![:)](http://www.autohotkey.com/board/public/style_emoticons/default/happy.png)
#12
-
Posted 23 October 2006 - 08:33 AM
![Put here requests of problems with regular expressions: post #12](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
1.s ^([ \t]*)(.+)([ \t]*)$
1.r \2
2.s ^([ \t]*)(.+)
2.r \2
3.s (.+)([ \t]*)$
3.r \1
1.r \2
2.s ^([ \t]*)(.+)
2.r \2
3.s (.+)([ \t]*)$
3.r \1
#13
-
Posted 23 October 2006 - 08:40 AM
![Put here requests of problems with regular expressions: post #13](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
Dear majkinetor, ![:)](//www.autohotkey.com/board/public/style_emoticons/default/happy.png)
I do not understand it ...
Can you kindly fill in the above example from which I can test it?
Please...
![:)](http://www.autohotkey.com/board/public/style_emoticons/default/happy.png)
I do not understand it ...
String := " The Quick Brown Fox " MsgBox, % RegExReplace(String, ... ) ; AllTrim MsgBox, % RegExReplace(String, ... ) ; LTrim MsgBox, % RegExReplace(String, ... ) ; RTrim
Can you kindly fill in the above example from which I can test it?
Please...
#14
-
Posted 23 October 2006 - 08:51 AM
![Put here requests of problems with regular expressions: post #14](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
Here is my take at Goyyah's first request, I end with something close of the first solution given by Titan for the pipe, but I use something else that I thought would be cool to introduce, bound assertions for start and end of string in a multiline string.
Unlike Titan, I remove only one char, ie. if the string ends as "foo|||", you will get "foo||". That can be what you want or not.
If you want to trim out all chars, just replace the ? after the char to remove with a *.
[EDIT] Meanwhile, Goyyah posted another request. :-)
Well, TrimChars should work to trim spaces (give \s as string to search), it is trivial to change to do LTrim and RTrim. Goyyah, I let you try and make the change... ;-)
MsgBox % ( Join TrimChar("|Apples|Bananas|Cherries|", "\|") . "`n" . TrimChar("Apples|Bananas|Cherries|", "\|") . "`n" . TrimChar("|Apples|Bananas|Cherries", "\|") . "`n" . TrimChar("Apples|Bananas|Cherries", "\|") ) MsgBox % ( Join ">" . TrimNewline("`nApples`nBananas`nCherries`n") . "<`n>" . TrimNewline("Apples`nBananas`nCherries`n") . "<`n>" . TrimNewline("`nApples`nBananas`nCherries") . "<`n>" . TrimNewline("Apples`nBananas`nCherries") . "<" ) TrimChar(_string, _char) { Return RegExReplace(_string , "^" . _char . "?(.*?)" . _char . "?$" , "$1") } TrimNewline(_string) { Return RegExReplace(_string , "\A\n?(.*?)\n?\z" , "$1", "ms") ; Multiline, DotAll }As shown, if the char is a RE special char, it must be escaped.
Unlike Titan, I remove only one char, ie. if the string ends as "foo|||", you will get "foo||". That can be what you want or not.
If you want to trim out all chars, just replace the ? after the char to remove with a *.
TrimChars(_string, _char) { Return RegExReplace(_string , "^" . _char . "*(.*?)" . _char . "*$" , "$1") } TrimNewlines(_string) { Return RegExReplace(_string , "\A\n*(.*?)\n*\z" , "$1", "ms") }Also note that you can write either \n or `n in the expression: the first one is substituted by the PCRE engine, the second one is substitued by AutoHotkey.
[EDIT] Meanwhile, Goyyah posted another request. :-)
Well, TrimChars should work to trim spaces (give \s as string to search), it is trivial to change to do LTrim and RTrim. Goyyah, I let you try and make the change... ;-)
#15
-
Posted 23 October 2006 - 08:57 AM
![Put here requests of problems with regular expressions: post #15](http://www.autohotkey.com/board/public/style_images/ortem/icon_share.png)
![Posted Image](http://autohotkey.net/~PhiLho/KM64.png)