AutoHotkey Community

It is currently May 27th, 2012, 9:40 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 789 posts ]  Go to page 1, 2, 3, 4, 5 ... 53  Next
Author Message
PostPosted: October 22nd, 2006, 3:18 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
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.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 6:21 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
How to remove a character that should not exist in beginning/end of string?

Here is a classic example:

Code:
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() :
Code:
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:

Code:
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 :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Last edited by SKAN on October 23rd, 2006, 11:19 am, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 6:34 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Goyyah wrote:
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.
The pipe char needs to be escaped so: MsgBox % RegExReplace("|Apples|Bananas|Cherries|", "^\|?(.*)\|+$", "$1")
Try removing the first and/or last pipe chars, it still works.

PhiLho, I apologize if you wanted to be the only supporter here.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 7:08 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Dear Titan, :)

The third one fails .. Should be a small tweak, which I am not able to guess.
Code:
MsgBox % RegExReplace("|Apples|Bananas|Cherries|", "^\|?(.*)\|+$", "$1")
MsgBox % RegExReplace("Apples|Bananas|Cherries|" , "^\|?(.*)\|+$", "$1")
MsgBox % RegExReplace("|Apples|Bananas|Cherries" , "^\|?(.*)\|+$", "$1")
MsgBox % RegExReplace("Apples|Bananas|Cherries"  , "^\|?(.*)\|+$", "$1")


Regards, :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 7:43 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Sorry my regex wasn't thorough enough, this works: ^\|*(.*[^\|])\|*$

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 7:50 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Thanks Titan, it works! :D
How do I use a `n ( linefeed) instead of a pipe? :roll:

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 8:10 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Goyyah wrote:
How do I use a `n ( linefeed) instead of a pipe?
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")

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 8:19 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Titan wrote:
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

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 8:29 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Goyyah wrote:
I am learning!
regular-expressions.info is a great place to start.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2006, 9:06 pm 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Titan wrote:
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!

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2006, 9:29 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Titan wrote:
PhiLho, I apologize if you wanted to be the only supporter here.
Certainly not! I am not the only one knowing REs, fortunately, so any contribution is welcome!

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2006, 9:33 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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:
Code:
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 :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Last edited by SKAN on October 23rd, 2006, 11:02 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2006, 9:40 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
1.s ^([ \t]*)(.+)([ \t]*)$
1.r \2


2.s ^([ \t]*)(.+)
2.r \2


3.s (.+)([ \t]*)$
3.r \1

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2006, 9:51 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Dear majkinetor, :)

I do not understand it ...

Code:
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...

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 23rd, 2006, 9:57 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
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.
Code:
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 *.
Code:
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... ;-)

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 789 posts ]  Go to page 1, 2, 3, 4, 5 ... 53  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Maestr0, migz99, tomoe_uehara and 58 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group