Unable to match \R when its surrounded with \s* ?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
NorInd
Posts: 43
Joined: 03 May 2020, 04:23

Unable to match \R when its surrounded with \s* ?

04 Jan 2023, 14:48

## problem-given_situation
I am trying to do the following regex copy_Reduced := RegExReplace(copy_M, "m`a)\s*\R\s*", " __ ")
I am "expecting output with all newlines (and surrounding \s*) removed"

## problem-arise_problem
The result shows it does not match anything.
- (in PCRE regex101 it does work as expected, in Ahk it does not)

Why is that?

## problem-other_ways_that_produce_expected_result__but_not_understood
It only works if I replace \R
with `n (can have / not have `a option)
or with $\R (must have `a option)

(Why is that?)

## problem-ex_code

Code: Select all

^r::
copy_M =
(
this is a testing string
  one-shot
more paragraphs 1
more paragraphs 2
)

; with `a #####
copy_Reduced := RegExReplace(copy_M, "m`a)\s*\R\s*", " __ ") ; Wrong, expecting output with all newlines (and surrounding \s*) removed <<<<<
displayAhkOutputWindow(copy_Reduced) 

copy_Reduced := RegExReplace(copy_M, "m`a)\s*`n\s*", " __ ") ; Ccorrect
displayAhkOutputWindow(copy_Reduced)

; ;ignore; copy_Reduced := RegExReplace(copy_M, "m`a)\s*`r`n\s*", " __ ") ; Wrong
; ;ignore; displayAhkOutputWindow(copy_Reduced)
; ;ignore; 
; ;ignore; copy_Reduced := RegExReplace(copy_M, "m`a)\s*`r\s*", " __ ") ; Wrong
; ;ignore; displayAhkOutputWindow(copy_Reduced)

copy_Reduced := RegExReplace(copy_M, "m`a)\s*$\R\s*", " __ ") ; Correct
displayAhkOutputWindow(copy_Reduced)

copy_Reduced := RegExReplace(copy_M, "m`a)\s*$`n\s*", " __ ") ; Correct
displayAhkOutputWindow(copy_Reduced)

copy_Reduced := RegExReplace(copy_M, "m`a)\R", " __ ") ; Correct (just for compare)
displayAhkOutputWindow(copy_Reduced)



; with no `a #####
; ;ignore; copy_Reduced := RegExReplace(copy_M, "m)\s*\R\s*", " __ ") ; Wrong
; ;ignore; displayAhkOutputWindow(copy_Reduced) 
; ;ignore; 
; ;ignore; copy_Reduced := RegExReplace(copy_M, "m)\s*`n\s*", " __ ") ; Correct
; ;ignore; displayAhkOutputWindow(copy_Reduced)
; ;ignore; 
; ;ignore; ; ;ignore; copy_Reduced := RegExReplace(copy_M, "m)\s*`r`n\s*", " __ ") ; Wrong
; ;ignore; ; ;ignore; displayAhkOutputWindow(copy_Reduced)
; ;ignore; ; ;ignore; 
; ;ignore; ; ;ignore; copy_Reduced := RegExReplace(copy_M, "m)\s*`r\s*", " __ ") ; Wrong
; ;ignore; ; ;ignore; displayAhkOutputWindow(copy_Reduced)

copy_Reduced := RegExReplace(copy_M, "m)\s*$\R\s*", " __ ") ; Wrong (when `a is not used)
displayAhkOutputWindow(copy_Reduced)
; ;ignore; 
; ;ignore; copy_Reduced := RegExReplace(copy_M, "m)\s*$`n\s*", " __ ") ; Wrong
; ;ignore; displayAhkOutputWindow(copy_Reduced)
; ;ignore; 
; ;ignore; copy_Reduced := RegExReplace(copy_M, "m)\R", " __ ") ; Correct (just for compare)
; ;ignore; displayAhkOutputWindow(copy_Reduced)

return

; [Outputs, printed by displayAhkOutputWindow()] #########
output = 
(
this is a testing string
  one-shot
more paragraphs 1
more paragraphs 2

  2 >>>>>
this is a testing string __ one-shot __ more paragraphs 1 __ more paragraphs 2

  3 >>>>>
this is a testing string __ one-shot __ more paragraphs 1 __ more paragraphs 2

  4 >>>>>
this is a testing string __ one-shot __ more paragraphs 1 __ more paragraphs 2

  5 >>>>>
this is a testing string __   one-shot __ more paragraphs 1 __ more paragraphs 2

  6 >>>>>
this is a testing string
  one-shot
more paragraphs 1
more paragraphs 2
)
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Unable to match \R when its surrounded with \s* ?

04 Jan 2023, 16:31

I'm not sure why the first example doesn't work, but you can use this way:

Code: Select all

str1 := "abc `n def"
str2 := "abc`r`ndef"
Loop 2
   MsgBox % RegExReplace(str%A_Index%, "\h*\R\h*", " __ ")
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Unable to match \R when its surrounded with \s* ?

04 Jan 2023, 16:42

its a PCRE bug https://www.pcre.org/original/changelog.txt
Version 8.31 06-July-2012 wrote:25. \s*\R was auto-possessifying the \s* when it should not, whereas \S*\R
was not doing so when it should - probably a typo introduced by SVN 528
(change 8.10/14).
recompiling with 8.31 fixes it. u were 1 version off lol. how tf u even find these bugs

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn and 166 guests