NewsLab
Jun 28 15:45 UTC

Regular expressions that work “everywhere” (johndcook.com)

83 points|by ColinWright||32 comments|Read full story on johndcook.com

Comments (32)

32 shown
  1. 1. Resonix||context
    why I built this
  2. 2. greazy||context
    I think you forgot to post a link?
  3. 3. codetiger||context
    I think he meant the post title. As the author shows his work finally in the article.
  4. 4. MathMonkeyMan||context
    I've always been a stickler for being specific about which regex language your thing accepts, and whether it is to match any substring, or a prefix, or a suffix, or the whole thing, or a line, or a substring of a line, or whatever.

    Here are some of the [more popular][1] ones, and then there are PCRE and Python.

    It took me a while to learn that some of the older ones you see in e.g. grep are [specified by POSIX][2].

    [1]: https://cppreference.com/cpp/regex#Regular_expression_gramma...

    [2]: https://pubs.opengroup.org/onlinepubs/009696899/basedefs/xbd...

  5. 5. LoganDark||context
    > the special characters . * ^ $

    These already do not work in many tools which require those special characters to be escaped to have any meaning. An easy example is GNU grep, sed, etc. which use BRE ("Basic Regular Expressions") by default. The article mentions GNU coreutils but does not explain that `-E` is required to fix that behavior.

  6. 6. agnishom||context
    A while ago, we wrote a paper about finding regexes which match the same way in both the greedy semantics and the leftmost maximal semantics.

    https://par.nsf.gov/servlets/purl/10534654

  7. 7. JdeBP||context
    The author is circling around, but not quite reaching, a statement that POSIX Basic Regular Expressions work everywhere, with the caveat that that not everyone has caught up with version 8 of the Single Unix Specification, which has slightly changed BREs.
  8. 8. quotemstr||context
    It drives me nuts when a developer documents something or other as being a "regex" but doesn't mention which dialect of regulation expression he's talking about. This habit is particularly common in the Rust, JavaScript, and Python communities, which seem to forget that their language's regular expression language isn't universal.
  9. 9. zahlman||context
    Why? Of course it means the dialect that is most directly supported by that language (by builtins or the standard library). And why should they have to consider other dialects? They aren't reading regexes from user input (or they'd be a lot more concerned about sanitization, catastrophic backtracking etc.), and their fellow developers all grok the conventions.
  10. 10. bartread||context
    I’d imagine precisely because they might be collecting regexes from user input such as parameter values or search terms, and the user may not know or care which technology your tool or service is built with. However, they will need to know which regex dialect(s) you support.

    And I’d further bet that people who are casual about specifying that are relatively strongly correlated with people who are casual about santization, catastrophic backtracking, etc. (At least based on code I’ve seen over the decades.)

  11. 11. quotemstr||context
    Because I don't know what language your program is even written in! Why should I know or care that you chose, e.g. TypeScript, when I'm trying to use or configure your program and don't know how to spell this or that regex concept?
  12. 12. xigoi||context
    Same applies to “Markdown”.
  13. 13. jonstewart||context
    Then there’s not just the issue of whether the engine supports a particular syntactical feature but the issue of matching semantics. Perl/PCRE’s semantics are far different from POSIX’s and some implementations different semantics altogether (and quite reasonably).
  14. 14. myroon5||context
    JSON schema's docs also have a recommended regular expression subset:

    https://json-schema.org/understanding-json-schema/reference/...

  15. 15. ok_dad||context
    Go stdlib regexp package does not support back references, as it uses the RE2 engine. You can use them in replace but not matching.
  16. 16. masklinn||context
    Regexp does not use re2, it is a separate implementation of the same concepts.
  17. 17. pmarreck||context
    I've become a fan of whatever PCRE2 understands
  18. 18. codetiger||context
    I built my Rust library for JSONLogic and use bindings for other languages after similar frustrations with Rule engines, template engines and IFTTT engines. https://github.com/GoPlasmatic/datalogic-rs
  19. 19. rtpg||context
    Emacs in particular I suffer so much from basically guessing what needs to be escaped or not. I know `rx` exists[0] as an alternative but it's not really fun to use.

    Even beyond the regex syntax itself, you often also start running into encoding problems when trying to actually use them. Typing the regex in a shell? Make sure to esacpe stuff properly. Regex in Python? Make sure it's a raw string. Etc etc etc

    It's a modern miracle we're at least within rhyming distance of how to write regexes in most tools.

    [0]: https://www.gnu.org/software/emacs/manual/html_node/elisp/Rx...

  20. 20. frou_dh||context
    Grasping at straws, it's kinda convenient that ( and ) match literally if the text being searched is Elisp code!
  21. 21. brookst||context
    Even more fun writing python that generates shell scripts that contain regex, and other nested-different-escaping scenarios.
  22. 22. galaxyLogic||context
    2 RegExp problems:

    1. You can not compose a bigger regexp out of smaller ones

    2. A regexp can not "call" other regexps

  23. 23. wwind123||context
    To do regex matching efficiently, you need to compile the pattern before using it. That'd exclude dynamically "calling" other regex patterns. But bigger regex pattern strings can be composed from smaller regex pattern strings. You'd just need to do the composition before the compilation.
  24. 24. ystlum||context
    Also define blocks if all someone wants is to break the pattern up to make it more readable.
  25. 25. woadwarrior01||context
    Swift has a RegexBuilder[1][2] interface, in addition to the usual string-ey interface that allows composition.

    [1]: https://github.com/swiftlang/swift-evolution/blob/main/propo...

    [2]: https://developer.apple.com/documentation/regexbuilder

  26. 26. tonyg||context
    That's one of the reasons RFC 9485, "I-Regexp: An Interoperable Regular Expression Format", is important.

    https://datatracker.ietf.org/doc/html/rfc9485

  27. 27. K0IN||context
    So my favorite regex (.*?) works? Puh.
  28. 28. dorianmariecom||context
    why the "?" ?
  29. 29. mrbluecoat||context
    > the following features work everywhere. YMMV.

    Amusing pair of statements.

  30. 30. brookst||context
    It works 100% of the time, 50% of the time.
  31. 31. gilrain||context
    We must find a way to return to SNOBOL/PITBOL. It’s so elegant and effective in Ada (where it’s in the standard library).

    https://en.wikipedia.org/wiki/SNOBOL

    > In the 1980s and 1990s, its use faded as newer languages such as AWK and Perl made string manipulation by means of regular expressions fashionable. SNOBOL4 patterns include a way to express BNF grammars, which are equivalent to context-free grammars and more powerful than regular expressions. The "regular expressions" in current versions of AWK and Perl are in fact extensions of regular expressions in the traditional sense, but regular expressions, unlike SNOBOL4 patterns, are not recursive, which gives a distinct computational advantage to SNOBOL4 patterns.

  32. 32. dekdrop||context
    I want to share Russ Cox's webpage on regexp https://swtch.com/~rsc/regexp/

    I find it a good reading.