Count instances of path (stored in string) within larger string

Get help with using AutoHotkey and its commands and hotkeys
JemyM
Posts: 44
Joined: 04 Jul 2017, 11:57

Count instances of path (stored in string) within larger string

27 Sep 2018, 14:40

I am trying to count how many times a path (stored within a variable) appears within a longer string (also stored within a variable).

I am aware of the RegExReplace() trick where you can count instances with RegExReplace(text,var,var,count), however, the content of the variable here is a path that contains characters such as \ and . that is interpreted wrong. Any idea how I can circumvent this?
User avatar
TheDewd
Posts: 1408
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Count instances of path (stored in string) within larger string

27 Sep 2018, 14:41

Can you provide an example of the variables?
Image Bulldozer - Sokoban inspired game from 1994 recreated in AutoHotkey.
Guest

Re: Count instances of path (stored in string) within larger string

27 Sep 2018, 15:18

You can count with StrReplace() or StringReplace as well, see OutputVarCount or UseErrorLevel (depending if you want to use the function or the command)
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Count instances of path (stored in string) within larger string

27 Sep 2018, 15:32

Code: Select all

haystack := "C:\path\to\file.txt C:\path\to\file.txt"
needle := "C:\path\to\file.txt"
RegExReplace(haystack, "\Q" needle "\E",, replacedCount)
MsgBox % replacedCount
Escaped characters - https://autohotkey.com/docs/misc/RegEx- ... ndamentals
JemyM
Posts: 44
Joined: 04 Jul 2017, 11:57

Re: Count instances of path (stored in string) within larger string

30 Sep 2018, 04:15

After working with this for awhile, I realized that the \Q \E option works poorly, because paths may contain \E. For instance, the following path was escaped prematurely.

C:\Games\Escape from Delirium (1996).zip;DELIRIUM.EXE;9850252;206C9B78|
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Count instances of path (stored in string) within larger string

30 Sep 2018, 04:34

From:
jeeswg's RegEx tutorial (RegExMatch, RegExReplace) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28031

Code: Select all

;prepare a literal needle:
;12 characters that need escaping in RegEx generally: \.*?+[{|()^$
vNeedle := RegExReplace(vNeedle, "[\Q\.*?+[{|()^$\E]", "\$0")

;also:
vNeedle := "\Q" RegExReplace(vNeedle, "\\E", "\E\\E\Q") "\E"
Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Alguimist
Posts: 371
Joined: 05 Oct 2015, 16:41
Contact:

Re: Count instances of path (stored in string) within larger string

30 Sep 2018, 04:40

Guest wrote:You can count with StrReplace()

Code: Select all

Haystack := "C:\path\to\Efile.txt|C:\path\to\File.zip|C:\path\to\Efile.txt"
Needle := "C:\path\to\Efile.txt"

StrReplace(Haystack, Needle,, Count)
MsgBox % Count

Return to “Ask For Help”

Who is online

Users browsing this forum: TAC109 and 32 guests