from Hacker News

Compile time regular expression in C++

by Tideflat on 9/12/23, 11:00 PM with 29 comments

  • by burntsushi on 9/14/23, 7:50 PM

    I'd love for someone to add this to rebar[1] so that we can get a good sense of how well it does against other general purpose regex engines. It will be a little tricky to add (since the build step will require emitting a C++ program and compiling it), but it should be possible.

    [1]: https://github.com/BurntSushi/rebar

  • by daemin on 9/14/23, 11:59 PM

    It's a good library but be careful as it can significantly increase compile times. I added a couple of reasonably long regular expressions to a c++ source file and it increased the compile time from near instant (below to 1 second) to 30 seconds. Might be wise to move these to dedicated source files so you don't pay this penalty each time you make changes.
  • by nly on 9/14/23, 11:08 PM

    Boost Xpressive has had static/"compile time" regex in C++ since the mid 2000s

    https://www.boost.org/doc/libs/1_83_0/doc/html/xpressive.htm...

    A performance comparison would be interesting.

  • by arunc on 9/15/23, 4:06 AM

    D has ctRegex since a decade ago https://dlang.org/phobos/std_regex.html#ctRegex
  • by arijun on 9/14/23, 11:57 PM

    At the risk of sounding like an idiot, what is the benefit of being able to match at compile time?
  • by cracauer on 9/14/23, 8:24 PM

    C++ finally catches up to Perl :-)
  • by emmanueloga_ on 9/14/23, 10:20 PM

    Since people is posting other lang implementations... someone did it for zig too (probably less polished than this C++ lib) [0]. It is nice that the regexes can be used at compile time too [1].

    --

    0: https://github.com/alexnask/ctregex.zig

    1: I think the difference between C++ template language and Zig comptime is that Zig's comptime is almost equal as Zig's regular language, whereas the experience of programming C++ templates almost feels like learning a separate, equally complex language.

  • by nemoniac on 9/14/23, 9:14 PM

    This has been available in Lisp since at least 2004.

    https://github.com/edicl/cl-ppcre/

  • by docandrew on 9/14/23, 7:19 PM

    This has been a part of the dlang standard library for some time: https://dlang.org/phobos/std_regex.html
  • by ape4 on 9/14/23, 7:12 PM

    Hopefully the standard regex will be constexpr soon.