Modify

#2663 closed Feature Request (Completed)

StringRegExpReplace backreference not working

Reported by: BrewManNH Owned by: BrewManNH
Milestone: 3.3.11.4 Component: AutoIt
Version: Severity: None
Keywords: RegExp backreference Cc:

Description

The only backreferences working in RegExp functions are \1\2\3... and $1$2$3..., the \g and \k backreferences mentioned in the help file don't work. If you use \g1 for example you'll get "g1" in the string instead of the backreferenced match.

This works:

Local $sOutput = StringRegExpReplace("Slide1.jpg", "([a-zA-Z]+)(\d{1})(\..+)", "${1}0$2$3") ;-- add a 0 before single digit
MsgBox(4096, "Test", $sOutput)

This does not:

Local $sOutput = StringRegExpReplace("Slide1.jpg", "([a-zA-Z]+)(\d{1})(\..+)", "\g{1}0\g2\g3") ;-- add a 0 before single digit
MsgBox(4096, "Test", $sOutput)

The first MsgBox gives you the output of "Slide01.jpg", the second message box gives you "g{1}0g2g3".

If you tried using a named group using (?<one>) and back referenced it using \k<one> you'll get "k<one>01.jpg"

Local $sOutput = StringRegExpReplace("Slide1.jpg", "([a-zA-Z]+)(?<one>\d{1})(\..+)", "\k<one>0\2\3") ;-- add a 0 before single digit
MsgBox(4096, "Test", $sOutput)

I tested this in 3.3.11.3, 3.3.10.2 and 3.3.8.1, doesn't work in any of them.

Attachments (0)

Change History (9)

comment:1 by DXRW4E, on Feb 27, 2014 at 1:22:18 AM

why you need the group 1 ??

$sOutput = StringRegExpReplace("Slide1.jpg", "[a-zA-Z]+\K(\d{1})(\..+)", "0$1$2") ;-- add a 0 before single digit
ConsoleWrite($sOutput & @LF)

$sOutput = StringRegExpReplace("Slide1.jpg", "[a-zA-Z]+\K(?=\d\..+)", "0") ;-- add a 0 before single digit
ConsoleWrite($sOutput & @LF)

$sOutput = StringRegExpReplace("Slide1.jpg", "[a-zA-Z]+\K(?=\d\..+)", "0$0") ;-- add a 0 before single digit
ConsoleWrite($sOutput & @LF)

$sOutput = StringRegExpReplace("Slide1.jpg", "(?<!\d)([1-9])(?=\h*\.[^\.]*$)", "0$1") ;-- add a 0 before single digit
ConsoleWrite($sOutput & @LF)

;; I believe the correct way will have to be
$sOutput = StringRegExpReplace("Slide10.jpg", "(?<!\d)([1-9])(?=\h*\.[^\.]*$)", "0$1") ;-- add a 0 before single digit
ConsoleWrite($sOutput & @LF)

comment:2 by jchd18, on Feb 27, 2014 at 5:31:43 AM

There is no bug here, at most a feature request.
\1 and up or $1 are not PCRE backreferences. They are a way to specify replacement of captured strings in the replacement pseudo-pattern: this is not something performed by PCRE itself, but by the wrapper around it (PCRE API doesn't have a replace function).

All forms of PCRE back-reference work inside the pattern, also references to subroutines, recursion and all bells and whistles.

comment:3 by Jpm, on Feb 27, 2014 at 7:59:13 AM

Type: BugFeature Request

Definitly is not a bug as \g \k concern only the pattern as describe in the doc.

Not sure it can be extend to replace parameter of StringRegExpReplace()

I move it to feature Request for Jon analysis

comment:4 by TicketCleanup, on Feb 27, 2014 at 8:00:02 AM

Version: 3.3.11.3

Automatic ticket cleanup.

comment:5 by BrewManNH, on Feb 27, 2014 at 5:31:55 PM

If they're not back references then why are they in the backreference section of the help file, and the link in the help file marked "complete description of PCRE patterns"?

If they can't be used in StringRegExpReplace, it should be documented, and if they don't work at all, they should be fixed.

comment:6 by jchd18, on Feb 28, 2014 at 10:26:36 PM

StringRegExpReplace uses a PCRE pattern for matching and a separate replacement string specification for actual replace. The latter doesn't follow PCRE pattern specifications. AutoIt devs decided to allow for \1 and $1 but they could have adopted a completely different syntax.

All PCRE back-reference syntaxes are accepted in the PCRE pattern part. Only \i and $i with i a positive integer are accepted in the replacement specification string (which is not processed by any PCRE function).

comment:7 by BrewManNH, on Mar 1, 2014 at 10:42:33 PM

Now the fog is clearing.

Thanks for that explanation.

The documentation for StringRegExp[Replace] needs to be modified then to make it clear that they aren't technically back references but are replacement patterns that just happen to look the same as the back references.

comment:8 by guinness, on Mar 1, 2014 at 11:23:48 PM

You have access to edit this information.

comment:9 by BrewManNH, on Mar 23, 2014 at 8:42:13 PM

Milestone: 3.3.11.4
Owner: set to BrewManNH
Resolution: Completed
Status: newclosed

Changed by revision [9992] in version: 3.3.11.4

Modify Ticket

Action
as closed The owner will remain BrewManNH.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.