RegExMatch AHK v1/v2: DotAll: normally, it does not match newlines

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SAbboushi
Posts: 252
Joined: 08 Dec 2014, 22:13

RegExMatch AHK v1/v2: DotAll: normally, it does not match newlines

13 Jun 2018, 02:13

Maybe I'm sleep deprived...

Documentation says that dot "normally, it does not match newlines".

With the following code:

Code: Select all

Pos := RegExMatch("xyz`nabc`n", "^.*$" , match)
In AHK v1, Pos = 1 and match = "xyz
abc"

In AHK v2, I get the results I expected: Pos = empty, match = empty

Am I missing something?
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: RegExMatch AHK v1/v2: DotAll: normally, it does not match newlines

13 Jun 2018, 12:59

Pos will be 0 if there is no match in v2. In v2, outputvar is a match object if there is a match.
By default, a dot matches any single character which is not part of a newline (`r`n) sequence,
Note, `r`n, not single `n. v2 docs says the same, it is probably not updated,
v2 changes wrote:RegEx newline matching defaults to (*ANYCRLF) and (*BSR_ANYCRLF); `r and `n are recognized in addition to `r`n.
Cheers.
NorInd
Posts: 43
Joined: 03 May 2020, 04:23

Re: RegExMatch AHK v1/v2: DotAll: normally, it does not match newlines

21 Oct 2022, 03:17

Helgef wrote:
13 Jun 2018, 12:59
Pos will be 0 if there is no match in v2. In v2, outputvar is a match object if there is a match.
By default, a dot matches any single character which is not part of a newline (`r`n) sequence,
Note, `r`n, not single `n. v2 docs says the same, it is probably not updated,
v2 changes wrote:RegEx newline matching defaults to (*ANYCRLF) and (*BSR_ANYCRLF); `r and `n are recognized in addition to `r`n.
Cheers.

Yes, in Ahk v1, better use `a flag option for your regex -- `a recognizes any type of newline

@ie,eg: see the following behavior:

Code: Select all

; >>>>>>>>> Run Code
stdout := FileOpen("*", "w")
; O) flag option is for getting match object

Pos := RegExMatch("xyz`nabc`n", "O)^.*$" , match)
stdout.WriteLine("-----")
stdout.WriteLine(". matches ``n")
stdout.WriteLine(Pos . ": " . match.Value)

Pos := RegExMatch("xyz`rabc`r", "O)^.*$" , match)
stdout.WriteLine("-----")
stdout.WriteLine(". matches ``r")
stdout.WriteLine(Pos . ": " . match.Value)

Pos := RegExMatch("xyz`r`nabc`r`n", "O)^.*$" , match)
stdout.WriteLine("-----")
stdout.WriteLine(". do not matches ``r``n")
stdout.WriteLine(Pos . ": " . match.Value)

Pos := RegExMatch("xyz`nabc`n", "`aO)^.*$" , match)
stdout.WriteLine("-----")
stdout.WriteLine(". do not matches ``n; cuz you use ``a) flag option -- ``a recognizes any type of newline => this is the desire behavior")
stdout.WriteLine(Pos . ": " . match.Value)

Pos := RegExMatch("xyz`nabc`n", "s`aO)^.*$" , match)
stdout.WriteLine("-----")
stdout.WriteLine(". do matches ``n; cuz you use s) flag option -- dotAll")
stdout.WriteLine(Pos . ": " . match.Value)

; >>>>>>>>> Output
; >"E:\AutoHotkey\AutoHotkey.exe" /ErrorStdOut "C:\Users\Zlgtx\Desktop\AHK scripts\Test\Test2.ahk"    
; -----
; . matches `n
; 1: xyz
; abc
; 
; -----
; . matches `r
; 1: xyz
; abc
; -----
; . do not matches `r`n
; 0: 
; -----
; . do not matches `n; cuz you use `a) flag option -- `a recognizes any type of newline => this is the desire behavior
; 0: 
; -----
; . do matches `n; cuz you use s) flag option -- dotAll
; 1: xyz
; abc
; 
; >Exit code: 0    Time: 0.2791

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, imbelicalcord, jollyjoe, Rohwedder and 246 guests