RegEx string literal

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By supaiku
:warning: Old Version Published before Godot 3 was released.

Hi,

the code editor is not so happy with my regex literal
E.g

myRegex.compile("/\d+/g") => Parser Error: Invalid escape sequence

Obviously I can’t escape the \ character here.

How can I make him swallow my regex?
(Godot 2.1 Beta)

What do you mean “Obviously I can’t escape the \ character here”? You actually need to escape, since this is no more than a regular string. Just use "/\\d+/g".

vnen | 2016-07-19 21:38

I tried it, but then the regex doesn’t work. regex.find(text) then regex.get_captures() # => []. But the text definitely contains numbers and it works for me here https://regex101.com/ That’s why I said obvious, because it seems to me to change the entire semantic.

supaiku | 2016-07-20 05:44

:bust_in_silhouette: Reply From: supaiku

For anyone who wonders, it’s a really simple fix. Just remove the //g from the expression.

Instead of this

"/\\d+/g"

do this

"\\d+"

This is kind of offtopic, but is it possible to get this regex working:
http://stackoverflow.com/questions/2648716/how-to-remove-bb-codes-from-a-string

#\[[^\]]+\]#

Or do you know if Godot’s RegEx system is limited to only the libraries and not the “general” RegEx? (IF that makes sense) :stuck_out_tongue:

wombatTurkey | 2016-07-24 20:21

the regex for matching bbcode could be

"\\[.*?\\]"

them just use the substitution method :slight_smile:

xDGameStudios | 2019-06-15 04:11