logoalt Hacker News

Regular expressions that work "everywhere"

47 pointsby ColinWrightlast Thursday at 12:13 PM21 commentsview on HN

Comments

JdeBPtoday at 2:11 AM

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.

agnishomtoday at 2:10 AM

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

rtpgtoday at 6:12 AM

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

show 1 reply
codetigertoday at 5:30 AM

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

MathMonkeyMantoday at 1:10 AM

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

myroon5today at 3:57 AM

JSON schema's docs also have a recommended regular expression subset:

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

galaxyLogictoday at 6:31 AM

2 RegExp problems:

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

2. A regexp can not "call" other regexps

show 1 reply
pmarrecktoday at 5:15 AM

I've become a fan of whatever PCRE2 understands

quotemstrtoday at 2:33 AM

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.

show 2 replies
ok_dadtoday at 4:31 AM

Go stdlib regexp package does not support back references, as it uses the RE2 engine. You can use them in replace but not matching.

show 1 reply
LoganDarktoday at 1:57 AM

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

jonstewarttoday at 3:22 AM

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

monkamonmetoday at 4:35 AM

[flagged]

ngruhntoday at 2:10 AM

[dead]

Resonixlast Thursday at 12:14 PM

why I built this

show 1 reply