Count lines with RegExpMatch

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
alfema
Posts: 32
Joined: 04 Sep 2015, 11:04

Count lines with RegExpMatch

19 Dec 2018, 14:41

Hello:

I know that it can be done with a loop, but I'm trying things with RegExpMatch, which I think it's possible to do, for example, I have the result of a router's nat table, which does not indicate the number of entries, only the list such that :

Code: Select all

NAT =
(
>
nat command
udp ...
upd ...
tcp ...
udp ...
>
)
I think that using O) and Match.Count () should get the result, but I do not understand how the Match objects are used. In https://regex101.com/ the search ]/^(udp)|^(tcp)/gm] return a number of entries in group 1 and another in 2, but that was reached.
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Count lines with RegExpMatch

19 Dec 2018, 14:55

maybe don't unterstand ... count lines

Code: Select all

NAT =
(
>
nat command
udp ...
upd ...
tcp ...
udp ...
>
)
stringsplit,h,nat,`n`r
msgbox,Total Lines=%h0%
return
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Count lines with RegExpMatch

19 Dec 2018, 15:50

StrReplace() can do it:

Code: Select all

NAT =
(
>
nat command
udp ...
upd ...
tcp ...
udp ...
>
)

MsgBox % lineCount(NAT)

lineCount(str) {
	StrReplace(str, "`n", "`n", count)
	return count + 1
}
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Count lines with RegExpMatch

19 Dec 2018, 16:56

Hi alfema
alfema wrote:I think that using O) and Match.Count () should get the result, but I do not understand how the Match objects are used.
try:

Code: Select all

NAT =
(Join`r`n
>
nat command
udp abc...
upd def...
tcp ghi...
udp jkl...
>
)
RegExMatch(NAT, "mO)^(udp)\s(\w+)|^(tcp)", NAT_Match)
; Note: the objects returned by RegExMatch (NAT_Match in this case) are read-only
MsgBox % NAT_Match.Count "`n" NAT_Match.2 "`n" NAT_Match.Value
alfema wrote:In https://regex101.com/ the search ]/^(udp)|^(tcp)/gm] return a number of entries in group 1 and another in 2, but that was reached.
AHk does not have the g (globabl) option. You would need to do this to find all the matches:

Code: Select all

NAT =
(Join`r`n
>
nat command
udp abc...
upd def...
tcp ghi...
udp jkl...
>
)
pos := 1
NAT_Match := ""
objNAT_Match := []
while pos := RegExMatch(NAT, "m)^(?:udp|tcp).+", NAT_Match, pos + StrLen(NAT_Match))
    MsgBox % objNAT_Match.Push(NAT_Match) "`n" NAT_Match
MsgBox % objNAT_Match.MaxIndex()
Others have already mentioned StrReplace. Much like StrReplace you can use RegExReplace. It can count the number of replacements that it makes. (see "OutputVarCount" https://autohotkey.com/docs/commands/Re ... Parameters)
alfema
Posts: 32
Joined: 04 Sep 2015, 11:04

Re: Count lines with RegExpMatch

21 Dec 2018, 14:10

Thanks to everyone for the answers, I could not count the number of udp / tcp lines with RegExpMatch, I will continue researching, finally I opted for:

Code: Select all

If RegExReplace (NAT, "(udp | tcp)", "", NATCount)
   MsgBox% NATCount
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Count lines with RegExpMatch

22 Dec 2018, 09:47

RegExMatch runs the pattern once against the whole string, which is starkly different to how regex101 handles its matching. to emulate this behavior a loop is required, which goes against the premise of ur OP

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 348 guests