Hacker News new | ask | show | jobs
by penguin359 602 days ago
Having compile-time RegEx's would certainly fix some issues I've had with them in Python. As they are just buried in plain strings, there's no validation of their syntax until you hit the line of code that tried to use them only to discover that Python 3.12 has changed the syntax and what was once working is now broken. I'd love to see this validated at compile-time/load-time of any language to avoid those unexpected surprises.
3 comments

If you need to detect syntax issues, you can use `re.compile()`. For example, before Python 3.11, you'll get an error:

    >>> re.compile(r'\w++')
    re.error: multiple repeat at position 3
To use such a compiled pattern, you can call the method on this object instead of `re`. For example:

    >>> word = re.compile(r'\w+')
    >>> word.findall('hello-there!')
    ['hello', 'there']
The existent of re.compile is compatible with and doesn’t address their complaint.
C# has compile-time regex, which are turned into fairly reasonable code doing the matching you can even inspect. The main goal isn't to have the syntax verified during compilation (technically it happens even during editing the code), but rather to reduce either dependencies on complex code at runtime, while still maintaining good performance by using the complex regex.
This article isn't about compile-time regex (which is in many languages, including javascript) though? It's compile-time regex MATCHING.
If you compile them outside the functions they will throw when imported