logoalt Hacker News

ogogmad • yesterday at 11:28 AM • 4 replies • view on HN

Autodidact here. Beyond just the five core quantifiers

  .
  |
  ?
  *
  +
regex only has capture groups

  ()
and lazy variants of the core quantifiers "?", "*" and "+", which are helpfully called:

  ??
  *?
  +?
The rest is just shorthands.

So while I think it's a worthwhile suggestion not to show people the whole thing the first time round, I doubt that the whole thing is really that complicated.


Replies

ryangittins • yesterday at 4:53 PM

> The rest is just shorthands.

If you really want to get technical: ., ?, and + are also shorthands. The formal definition of a regular expression is very minimal on purpose.

`.` is just `(a|b|c|...)` - List of all literal characters

`x?` is just `(x|)` - "x or empty string"

`x+` is just `xx*` - "x followed by zero or more x characters"

Whether this is exactly how they are implemented in any particular regex engine is an implementation detail, but formally there are very few operations which aren't just syntactic sugar.

StilesCrisis • yesterday at 1:11 PM

Regex has many more features than this, e.g. backreferences. I agree that it's easy to learn enough regex to be dangerous with that subset, but that's not everything regex offers.

➕ show 2 replies
nick__m • yesterday at 6:41 PM

You are barely scratching the surface! You forgot about positive and negative look ahead and behind, lookaround, backreferences, recursion, inline engine flags settings and probably some other things I forgotten about. PCRE2 compatible regexes are potentially monstrously complex things.

atoav • yesterday at 8:04 PM

I am a big believer in the idea that within any educational context learning is much easier if you start with the problem instead of describing the atoms that make up the solution. This way students will have a much easier way of understanding why they need to learn what they are learning. And then, once they know enough to be dangerous, you introduce more complicated variations and background ideas especially like yours.

Note that I teach tech topics at the university level, in an art school. That means a mixed audience of people who have a hard time with programming and complete nerds, but didn't come here for semantic definitions, but to get shit done. To get everybody on board with such a topic requires you to start small and on a literal "explain this to a 5-year old"-level.

Of course when teaching this to our sysadmin or someone more knowledgeable I could take your approach since I can assume they know the priors and even have seen a regex or two.

However most people don't care about any topic except when it solves their problems in ways other solutions couldn't. And this needs to be the first thing you show them. Why are regexes really awesome for certain problems that commonly exist. Bonus points if it is a problem they already encountered or are currently having.