Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Put here requests of problems with regular expressions


  • Please log in to reply
1074 replies to this topic
PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005
Should include in the test:
Loop C:\*.BMP,0,1
{
   imageFiles := imageFiles . A_LoopFileLongPath . "`n"
}
Loop C:\*.JPG,0,1
{
   imageFiles := imageFiles . A_LoopFileLongPath . "`n"
}
Loop C:\*.GIF,0,1
{
   imageFiles := imageFiles . A_LoopFileLongPath . "`n"
}
Probably faster, as the system returns only relevant files, instead of getting all files and filtering them after.
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
My test was RegExMatch Vs SplitPath .. :)
kWo4Lk1.png

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Yeah, I was thinking about PhiLhos test.

Probably faster, as the system returns only relevant files, instead of getting all files and filtering them after.

Probably faster ? :D
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

My test was RegExMatch Vs SplitPath .. :)

This test is based upon if..in and regex:
SetBatchLines, -1
Process, Priority, , R
dir = %A_MyDocuments%\*.*
i = 100

t1 := A_TickCount
Loop, %i% {
	Loop, %dir%, , 1
		If A_LoopFileExt in bmp,jpg,gif
			list = %list%%A_LoopFileFullPath%`n
	list := ""
}

t2 := A_TickCount
Loop, %i% {
	Loop, %dir%, , 1
		If RegExMatch(A_LoopFileExt, "^bmp|jpg|gif$", "i")
			list = %list%%A_LoopFileFullPath%`n
	list := ""
}

t3 := A_TickCount
MsgBox, % "if..in:`t" . t2 - t1 . "`nregex:`t" . t3 - t2
Seems like regex is faster!!1

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
@Titan: Unbelievable! :shock: I never knew A_LoopFileExt existed! :(
kWo4Lk1.png

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Look like we got superb tool to play with.

I can not get it how RegExp can be faster than any concrete string function, but this just proves that this re implementation is good and optmised.
Posted Image

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

this just proves that this re implementation is good and optmised.

What is it btw, pcre 6.7?

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
How to find the number of a particular character/string/whole word present in a string?

My basic need is find the number of delimiters in a string and would like to have a one line solution like:

;I want to ascertain the number of semi colons and add 1 to 
; find the no.of fields
FieldCount := RegEx??("Apples;Bananas;Cherries", ...) + 1

Refer this post: <!-- m -->http://www.autohotke... ... 4194#84194<!-- m -->

I would also like to know how the matches can be counted for a substring or a whole word

Please refer this topic : Count instances of a word appears in a file

:?:
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

How to find the number of a particular character/string/whole word present in a string?

Easy: MsgBox, % StrLen(RegExReplace("one; two; three; four", "[^;]", ""))

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

this just proves that this re implementation is good and optmised.

What is it btw, pcre 6.7?

I believe so.
Indeed, the code is highly optimized. For example, author doesn't hesitate to put similar code several time to avoid adding a test in the loop... And it uses goto when it saves some CPU cycles! :-)
I didn't saw that in PCRE code (but might have skipped it), but some implementations can detect that the RE is plain text. In this case, they just use an optimized Boyer-Moore string search...

Goyyah, I made a variant of the SplitPath functions, published in the other topic. They might not please you, as they don't use simple assignment, but I felt I had to test this possibility.
It seems latest beta (options in regex) broke the multiline (extended) REs, I will report that.

To answer your latest request is easy, Chris provided a OutputVarCount to the RegExReplace function:
str = Apples;Bananas;Cherries
RegExReplace(str, ";", "", count)
FieldCount := count + 1
MsgBox % FieldCount

str = Recommended for new scripts due to its superior speed and reliability.
RegExReplace(str, "\w+", "", count)
MsgBox % count

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

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Goyyah, I made a variant of the SplitPath functions, published in the other topic.


Thanks for the elaborate posting!
BTW, R: How to emulate StringSplit? should be R: How to emulate SplitPath?, I guess!
I am posting here because I do not want to mess that topic.
I would like to see that topic become sticky someday! :)

To answer your latest request is easy, Chris provided a OutputVarCount to the RegExReplace function


I get blank values! :roll:

:)
kWo4Lk1.png

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

MsgBox, % StrLen(RegExReplace("one; two; three; four", "[^;]", ""))


Works fine! Thanks!
How do I count "e;" from that string?

:)
kWo4Lk1.png

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

How do I count "e;" from that string?

That's quite tricky - it's like doing a StringReplace (All). One way is to: StrLen(RegExReplace("one; two; three; four" . " ", ".*?(?:(?<=e;)(.)|$)", "$1")) but it requires at least one character at the end of the string to catch ending delimiters.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

it requires at least one character at the end of the string to catch ending delimiters.


Oh! I did not guess that would be tough. Many thanks! :D
kWo4Lk1.png

PhiLho
  • Moderators
  • 6850 posts
  • Last active: Jan 02 2012 10:09 PM
  • Joined: 27 Dec 2005

I get blank values! :roll:

:?: I tested the above with the latest beta, did you got it? I can't remember if this count of replaces was in the previous version.

How do I count "e;" from that string?

Sorry, I don't understand. Do you mean RegExReplace(str, "e;", "", count)?
Posted Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")