Godot RegEx sub()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By display

I have a couple of questions about RegEx.
When I enter this pattern:

/.\w+/gm
Imgur
regex101_pattern

And I enter such a script in Godot

var regex=RegEx.new()
var NAME='smokebasic.tscn'
print( NAME )
print( 'SUB  ',  regex.sub(NAME,'/\\.\\w+/g') )

It outputs to me

smokebasic.tscn
SUB

The sub function does not work, I replaced \ with \ otherwise it shows me an error. Maybe I should type differently?

I think there’s an error here; firstly, the regex pattern should be compiled and then (secondly) used in the sub method.

docs

e.g.

var regex=RegEx.new()
regex.compile("pattern")
print(regex.sub(input_string, replacement_string))

I’m really not sure what you want to substitute here? The pattern matches the ‘.tscn’ (when ignoring the delimiters and flags).

spaceyjase | 2023-06-16 14:55

thanks! it works

display | 2023-06-18 17:43