0 votes

I've searched online and found some solutions, but none of them work.

In python, all you have to do is import re

From the documentation, in GDScript it looks like you do this:

var regex = RegEx.new()

But it's not working.

Specifically, this is the code (in python) that I want to use:

def getAgeRange(text):
    minString = text.split("-")[0].strip()
    maxString = text.split("-")[1].strip()
    match = re.match(r"([0-9]+)[\s+]?([a-z]+)", maxString)
    matchList = match.groups()
    max = matchList[0]
    max_units = matchList[1]

    match = re.match(r"([0-9]+)\s+([a-z]+)", minString)
    if match:
        matchList = match.groups()
        min = matchList[0]
        min_units = matchList[1]
        min = minString
        min_units = max_units
    print(str(min) + str(min_units) + "-" + str(max) + str(max_units))

An example of strings I would use would be 3-5 wks and 3 mo-6yo

in Engine by (12 points)

Does it work if you try another, simpler pattern? Godot uses the PCRE2 library, which may not support some specific regular expressions.

Regular expressions are not standardized and support varies across implementations.

Yep, I agree with Calinou here. There are a number of well-vetted and robustregex implementations, but they are not 100% syntactically compatible with each other. Generally, they can all accomplish the same or similar tasks, but you'll need to take the time to learn the nuances of the regex engine you're using.

Also, the host of the regex engine (Godot in this case) can introduce an additional layer of complication due to the escaping mechanisms that may be required to support the regex syntax within the scope of a host language.

Bottom line - working with regex is always a bit fiddly until you become comfortable with a specific implementation and environment.

Please log in or register to answer this question.

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