Live data from Hacker News

Show HN: Regex Cheatsheet

ihateregex.io

51–60 of 135 posts

Re: Show HN: Regex Cheatsheet

#51
post #41

Would be nice to have a regex for parsing HTML... grabs popcorn

With subroutines and recursive patterns I think you could do something parsing valid HTML. Your sanity won't be left intact tho.

how about this "match "A B C" where A+B=C"[1] for sanity?

[1] http://www.drregex.com/2018/11/how-to-match-b-c-where-abc-be...

Re: Show HN: Regex Cheatsheet

#52
post #39
post #11

This is really cool! 2 points: 1. it fiddled with my back button which is a bit annoying 2. a better email sample is ^[^@]+@[^@]+\.[^@]+$ which removes the 2 ampersands problem.

Even that is wrong because you can have privately owned TLDs (I forget what they're technically called) like .google So sundar.pichai@google is technically a valid address (whether .google has any MX records is another matter) Regex shouldn't really be used for email addresses anyway because the only reliable way to authenticate an email address is to literally send an email to that address.

.google does not have any MX records

Re: Show HN: Regex Cheatsheet

#53

I use regex a lot but deliberately keep it simple. One thing that confounded me often was positive and negative look-arounds. I always got the expressions mixed up, until I just put the expressions into a table like this... look-behind | look-ahead ------------------------------------ positive (? It's not hard, but for whatever reason my brain had trouble remembering the usage because every time I looked it up, each…

Maybe it's easier to remember that lookbehinds are evil from an implementation standpoint, and even in Perl have arbitrary limitations. If you see lookbehinds, look away! If you see lookaheads, go ahead.

Re: Show HN: Regex Cheatsheet

#58
post #21

Nobody pointed it out, but there's also https://regexr.com/ It's how I learned regex years ago, and I still use it today to test/build more complex patterns.

I love regexr. Has been a constant tab in my browser for years now.

Re: Show HN: Regex Cheatsheet

#59
post #34
post #21

Nobody pointed it out, but there's also https://regexr.com/ It's how I learned regex years ago, and I still use it today to test/build more complex patterns.

My go-to is https://regex101.com/

I've been using regex101 for many years and love it! The debugger [0] that it has is amazing!

[0] - https://regex101.com/debugger

Re: Show HN: Regex Cheatsheet

#60
OK, these kinds of regex tools get posted quite often. I get it, regex is very confusing at first. And some of these use-cases result in rather complex expressions nobody should be forced to write from scratch (you are still remembering to write unit tests for them though, right?)

But as someone who actually knows [some flavours of] regex fairly well, what I would really like, is a reference that covers all the subtle differences between the various regex engines, along with community-managed documentation (perhaps wiki pages) of which applications & API versions use which flavour of regex.

For example, the other day I wanted to run a find on my NAS. I needed to use a regex, but the Busybox version of find doesn't support the iregex option, so all expressions are case-sensitive. With some googling, I was able to find out that the default regex type is Emacs, but I wasn't able to find either a good reference for exactly what Emacs regex does and doesn't support, nor any information about how to set the "i" flag. In the end I had to manually convert every character into a class (like [aA] for "a") which was tedious, but quicker than trying to find a better solution or resorting to grep.

A related, annoyingly common pattern is that the documentation for `find` states that `--regex` specifies a regex, but it does not state which flavour of regex. The documentation for certain versions of `find`, which support alternative engines, note that the default is Emacs. From this I was able to infer (perhaps wrongly) that the Busybox `find` uses Emacs-flavoured regex, but ultimate I still had to resort to some trial-and-error. This problem is all too common in API documentation.

Post reply on HN