CC: Tweaked

CC: Tweaked

65M Downloads

Regexes

electrovoyage opened this issue ยท 2 comments

commented

Regexes (regular expressions) are really useful for getting certain parts of a string, for instance. Unfortunately, Lua only has its own extremely basic expressions, which lack many of the features of regexes.
I'm not sure if this would be possible, but I would like to propose a module or function in cc.strings that implements regexes, perhaps through Java's java.util.regex.Pattern. I've looked through all the cc.* modules and realized they're pure Lua, so I'm not sure if it's at all possible to create a Lua-importable Java module (perhaps through modifying require? though that's probably not the best solution), but it would be great to have regexes in any form

commented

Anything that can be done with a regex expression can be done with one or more Lua patterns.
Lua can do it, it just might take more steps.

Also, CC has the philosophy of doing things in Lua if they can be and only leaning on Java if it has to for functionality or critical performance.

commented

Thanks for the report! I'm afraid I'm not going to implement this, for a few reasons:

  • One of the issues with java.util.regex.Pattern is that it has no resource limits, so it's very susceptible to Regex DDoS. This means you'd have to roll your own regex engine instead, either with a fuel system, or a non-backtracking one, so that it's always O(n).
  • I'd rather avoid adding custom functionality that overlaps with existing Lua functionality.

If you are looking for a more fully-featured pattern matching system, I'd honestly recommend using one of the existing Lua libraries. LuLPeg is the de-facto library for parsing, but something like reLua might work for your use case.