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 

Put here requests of problems with regular expressions
Goto page 1, 2, 3 ... 15, 16, 17  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Sun Oct 22, 2006 3:18 pm    Post subject: Put here requests of problems with regular expressions Reply with quote

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.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5581

PostPosted: Sun Oct 22, 2006 6:21 pm    Post subject: Reply with quote

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 Smile
_________________
SKAN - Suresh Kumar A N


Last edited by SKAN on Mon Oct 23, 2006 11:19 am; edited 3 times in total
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5009
Location: imaginationland

PostPosted: Sun Oct 22, 2006 6:34 pm    Post subject: Reply with quote

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.
_________________

RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5581

PostPosted: Sun Oct 22, 2006 7:08 pm    Post subject: Reply with quote

Dear Titan, Smile

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, Smile
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5009
Location: imaginationland

PostPosted: Sun Oct 22, 2006 7:43 pm    Post subject: Reply with quote

Sorry my regex wasn't thorough enough, this works: ^\|*(.*[^\|])\|*$
_________________

RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5581

PostPosted: Sun Oct 22, 2006 7:50 pm    Post subject: Reply with quote

Thanks Titan, it works! Very Happy
How do I use a `n ( linefeed) instead of a pipe? Rolling Eyes
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5009
Location: imaginationland

PostPosted: Sun Oct 22, 2006 8:10 pm    Post subject: Reply with quote

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")
_________________

RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5581

PostPosted: Sun Oct 22, 2006 8:19 pm    Post subject: Reply with quote

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.. Very Happy
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
Titan



Joined: 11 Aug 2004
Posts: 5009
Location: imaginationland

PostPosted: Sun Oct 22, 2006 8:29 pm    Post subject: Reply with quote

Goyyah wrote:
I am learning!
regular-expressions.info is a great place to start.
_________________

RegExReplace("irc.freenode.net/autohotkey", "^(?=(.(?=[\0-r\[]*((?<=\.).))))(?:[c-\x73]{2,8}(\S))+((2)|\b[^\2-]){2}\D++$", "$u3$1$3$4$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5581

PostPosted: Sun Oct 22, 2006 9:06 pm    Post subject: Reply with quote

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!
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Mon Oct 23, 2006 9:29 am    Post subject: Reply with quote

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!
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 5581

PostPosted: Mon Oct 23, 2006 9:33 am    Post subject: Reply with quote

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

Question

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 Smile
_________________
SKAN - Suresh Kumar A N


Last edited by SKAN on Mon Oct 23, 2006 11:02 am; edited 1 time in total
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 3593
Location: Belgrade

PostPosted: Mon Oct 23, 2006 9:40 am    Post subject: Reply with quote

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


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


3.s (.+)([ \t]*)$
3.r \1
_________________
Back to top
View user's profile Send private message MSN Messenger
SKAN



Joined: 26 Dec 2005
Posts: 5581

PostPosted: Mon Oct 23, 2006 9:51 am    Post subject: Reply with quote

Dear majkinetor, Smile

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...
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Mon Oct 23, 2006 9:57 am    Post subject: Reply with quote

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. Smile
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... Wink
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
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, 3 ... 15, 16, 17  Next
Page 1 of 17

 
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