Modify

Opened 10 years ago

Closed 10 years ago

#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 Changed 10 years ago by DXRW4E

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 Changed 10 years ago by jchd18

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 Changed 10 years ago by Jpm

  • Type changed from Bug to Feature 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 Changed 10 years ago by TicketCleanup

  • Version 3.3.11.3 deleted

Automatic ticket cleanup.

comment:5 Changed 10 years ago by BrewManNH

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 Changed 10 years ago by jchd18

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 Changed 10 years ago by BrewManNH

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 Changed 10 years ago by guinness

You have access to edit this information.

comment:9 Changed 10 years ago by BrewManNH

  • Milestone set to 3.3.11.4
  • Owner set to BrewManNH
  • Resolution set to Completed
  • Status changed from new to closed

Changed by revision [9992] in version: 3.3.11.4

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The owner will remain BrewManNH.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.