RegExMatch with Mode 3 returns 0 occurances in string Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BurnerPhone
Posts: 12
Joined: 09 Mar 2021, 09:36

RegExMatch with Mode 3 returns 0 occurances in string

12 Apr 2021, 05:31

Code: Select all

String =
(
Lorem. ipsum dolor. sit 
amet, consectetur. 
adipiscing elit, sed. do 
eiusmod.
)

RegExMatch(String, "O)\.", dotArray)
numberOfDots := dotArray.Count()
msgbox % numberOfDots ; returns 0
Trying to count dots in clipboard and it always returns 0. Can't understand why it's not working.
Got a string with dots - check
Escaped dot as regex pattern - check
Array with mode 3 to return .count - check

Can anyone point out what's wrong?
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: RegExMatch with Mode 3 returns 0 occurances in string

12 Apr 2021, 05:43

You need to use () for subpattern:

Code: Select all

String =
(
Lorem. ipsum dolor. sit 
amet, consectetur. 
adipiscing elit, sed. do 
eiusmod.
)

RegExMatch(String, "O)(\.)?.*(\.)?.*(\.)?.*(\.)?.*(\.)?.*(\.)?.*(\.)?.*(\.)?.*", dotArray)
numberOfDots := dotArray.Count()
msgbox % numberOfDots ; returns 0
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExMatch with Mode 3 returns 0 occurances in string

12 Apr 2021, 05:50

Code: Select all

String =
(
Lorem. ipsum dolor. sit 
amet, consectetur. 
adipiscing elit, sed. do 
eiusmod.
)

RegExReplace(String, "\.",, numberOfDots)
msgbox % numberOfDots
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExMatch with Mode 3 returns 0 occurances in string  Topic is solved

12 Apr 2021, 05:55

Or

Code: Select all

StrReplace(String, ".",, numberOfDots)
msgbox % numberOfDots
BurnerPhone
Posts: 12
Joined: 09 Mar 2021, 09:36

Re: RegExMatch with Mode 3 returns 0 occurances in string

12 Apr 2021, 05:57

HotKeyIt wrote:
12 Apr 2021, 05:43
You need to use () for subpattern:

Code: Select all

String =
(
Lorem. ipsum dolor. sit 
amet, consectetur. 
adipiscing elit, sed. do 
eiusmod.
)

RegExMatch(String, "O)(\.)?.*(\.)?.*(\.)?.*(\.)?.*(\.)?.*(\.)?.*(\.)?.*(\.)?.*", dotArray)
numberOfDots := dotArray.Count()
msgbox % numberOfDots ; returns 0
Thank you for the answer.
But why it returns as much patters as you've posted inside RegExNeedle rather than dot count?
It returns 8, and there's only 5 dots in String.
BurnerPhone
Posts: 12
Joined: 09 Mar 2021, 09:36

Re: RegExMatch with Mode 3 returns 0 occurances in string

12 Apr 2021, 06:00

teadrinker wrote:
12 Apr 2021, 05:55
Or

Code: Select all

StrReplace(String, ".",, numberOfDots)
msgbox % numberOfDots
Haven't thought about using replace function without replace itself.
Thanks!
Still, I don't understand why RegExMatch doesn't work
User avatar
boiler
Posts: 16962
Joined: 21 Dec 2014, 02:44

Re: RegExMatch with Mode 3 returns 0 occurances in string

12 Apr 2021, 10:13

BurnerPhone wrote:
12 Apr 2021, 06:00
Still, I don't understand why RegExMatch doesn't work
As HotKeyIt pointed out, it’s because you haven’t identified any subpatterns.
RegExMatch documentation wrote:Match.Count(): Returns the overall number of subpatterns.
Even if you put parentheses around it, it wouldn’t give you a count because that just provides one matching subpattern. It is a count of the number of subpatterns that matched as in HotKeyIt’s example. RegExMatch doesn’t find multiple matches at once, so the count wouldn’t be multiple.
teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: RegExMatch with Mode 3 returns 0 occurances in string

12 Apr 2021, 10:26

To get all occurences of the pattern you have to use a cycle:

Code: Select all

String =
(
Lorem. ipsum dolor. sit 
amet, consectetur. 
adipiscing elit, sed. do 
eiusmod.
)
res := []
while RegExMatch(String, "O)\.", m, m ? m.Pos + m.Len : 1)
   res.Push(m[0])
MsgBox, % res.Count()

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 347 guests