0 votes
func why_is_this_not_working():
    var r := RegEx.new()
    r.compile("`(?<=var).*?one.*?(?=var|$)`s")
    var result = r.search("var one = 1 setget set_one, get_one")

Why is regex not working?
Tested on regex testing website and I'm supposed to get " one = 1 setget set_one, get_one"

What I'm working on:

Making my own doc generator

I'm trying to make a simple lexer (I think it's a lexer) that's getting a variable default value from a script (because theScript.get_property_default_value() is not working. I'm also trying to make one for function, because default arg from methods list doesn't return anything either).

If anyone got any better idea then feels free to suggest.

Godot version v3.4.2.stable.official [45eaa2daf]
in Engine by (79 points)
edited by

I assume you'll need to do some additional escaping for GDScript as mentioned in the docs. If that doesn't help, post some sample strings you're targeting and indicate exactly what you expect the regex to extract from them...

Yeah one thing I was thinking is that they are trying to use a Regular Expression for a different programming language in GDScript.

Using current pattern for a regex testing website with the exact same string. I'm expecting " one = 1 setget set_one, get_one" for this string. And for its main purpose is get any thing after var with then variable name one (which will be replace by %s later) is found until next token (var, func etc.)

I don't really know very much about Regular Expressions (GDScript or otherwise) or I would help, sorry.

1 Answer

0 votes

I'm not sure I understand the reason for the complexity of the regex you've constructed (especially the positive lookbehind elements). Really, it sounds like you just want to capture the content past the var. Maybe that's over-simplifying what you're actually after, but let's start there and see where it goes.

Really, something as simple as this should get you in the ballpark:

var r := RegEx.new()
r.compile("^ *var(.+)$")
var result = r.search("var one = 1 setget set_one, get_one")
print(result.get_string(1)) 
by (13,080 points)

The regex need to understands multiline token, like defining an array or object with big amount of args.
I've tried s delimiter "/.*/s" but regex class doesn't return anything.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.