Why is this regex pattern not working but work completely fine on multiple regex testing website?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Multirious
func why_is_this_not_working():
    var r := RegEx.new()
    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 the Script.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.

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…

jgodfrey | 2022-02-09 18:19

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

SF123 | 2022-02-09 19:24

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.)

Multirious | 2022-02-10 11:33

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

SF123 | 2022-02-10 21:47

:bust_in_silhouette: Reply From: jgodfrey

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")

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.

Multirious | 2022-02-11 12:44