regex back references not working

Godot Version

Godot 4.2.2

Question

I’m trying to do a back reference replacement but is not working

var a = "one two three four five"
var r = RegEx.new()
r.compile("\\w+")
var b = r.sub(a, "$1&", false)
print(b)

the “print(b)” returns an empty string

You missed a parenthesis i think. Try this:

var a = "one two three four five"
var r = RegEx.new()
r.compile("(\\w+)")
var b = r.sub(a, "$1&", false)
print(b)

OOOoooooh I get it you need to assign the expression to a group before they can be considered as such

Thanks a lot!

1 Like