Live data from Hacker News

Show HN: RegEx for Regular Folk – A visual, example-based introduction

refrf.shreyasminocha.me

51–60 of 113 posts

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#51
post #48

I personally had the most trouble with regexes because I didn't have a good mental model of how they worked. The hard part wasn't finding the correct symbol/character class I was trying to match, but coming to grips with repetition, greedy/nongreedy, etc. I took a compilers class in college where one of the projects was to implement a simple regex matcher using NFAs. Bashing my head against this for a week really hel…

I'm not sure how to explain it but the most important thing I've learned in over a decade of programming experience is to not use regular expressions for many things they may seem like a regular expression problem.

For example, even something as simple a phone number can have all sorts of weird but valid variants. Be sure you really need to even validate it's format and not just that it's present.

Trying to handle all of those variants via regex expression is doable but a pain. And in practice you as the programmer should not be defining those variants that are valid as it's up to the business itself to define what type of data it considers to be valid for the field.

That said I've also worked for companies with small engineering teams where the goal has always been to be as efficient with development time as possible, as opposed to making a near ideal system. Software has different needs when it's used by a thousand people than when it's used by millions.

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#52
This looks really nice but I think it suffers from the same issue a lot of regex tutorials suffer from. It's focusing solely on the regex and not at all on how to actually execute them. This site in particular says it's going to use javascript but at least the first few pages don't show anything except raw regular expressions.

For any tutorial about regular expressions I think the second thing (beyond a very simple example regex) to show should be how to actually execute one in code. Is it that all the tutorials want to be language-agnostic? Maybe just show a javascript example and point out which part is the js function/method call and which part is the actual regex.

It's nice to be told what /[aeiou]/ means but without actually typing it in and executing it (against various inputs, not just one) it wouldn't really sink in for me.

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#53
Great work! These examples are super clear.

One thought: it would be great to highlight a given match on hover. I know that each match has its own undertie and it's explicitly mentioned early on but it might help really drive it home if each match reacted individually when hovered.

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#54

This looks really nice but I think it suffers from the same issue a lot of regex tutorials suffer from. It's focusing solely on the regex and not at all on how to actually execute them. This site in particular says it's going to use javascript but at least the first few pages don't show anything except raw regular expressions. For any tutorial about regular expressions I think the second thing (beyond a very simple e…

Good. Unless you’re trying to write a one off script, you shouldn’t manually use them in real world projects. They’re a big source of bugs

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#55
I love this! I love RegEx but have struggled trying to “teach” others over the years. In addition to books like this, I often find writing RegEx with something like Expressions[1] (and I know there are many great website solutions, Expressions is just a great app that I find very approachable to newcomers) is a great way to learn. When you can see what you’re writing select what you want, you get a great grasp of how it works. This, alongside a good book with good examples, is pretty much how I learned RegEx ~12 years ago.

[1]: https://www.apptorium.com/expressions

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#56
This is a nice, non-threatening intro. One piece of advice that I've learned from teaching people regex syntax is that it's much easier to keep it to three basic topics at first (Repetition, Alternation, Concatenation), and then describe the rest of the stuff (character classes, character escapes, etc.) as "syntactic sugar" that makes those previous topics simpler, or provides more power. I usually introduce groups pretty early but most people get them notionally because they're kind of like algebraic parentheses. And then I'll expand on groups as well to show more power, escapes, etc.

For example,

(a|c|d|e|f|g|...|z) uses only notional groups and basic alternation while [abcdefghi...xyz] shows character classes, and [a-z] shows ranges - each step builds on the previous step and shows how to make them easier. For the learner this seems to act as building blocks rather than "separate things that are kind of alike I need to learn"

This is similar to how you can talk about repetition as

aaaaa, then aaaaaaaaaaaaa, then a, then aa, then a+, then a?, a{0,1}, a{0,5}, a{1,5}, a{,10}, etc. which simplifies, then generalizes the idea of repetition from a very natural concept build on concatenation to an opaque looking syntax that turns out to be both general and powerful

After that, most of the time I need to explain how capturing works, and how to turn it off and so on. Good tools help here and it starts to move away from a whiteboard exercise into something more active. But if students have followed you to this point it starts to make them feel very powerful as they're suddenly parsing things apart and transforming them.

At the end I usually follow up with a big on anchors (^ and $) and other odds and ends (case insensitivity, global search, greedy and non-greedy, etc.) and usually turn people loose after that. I've rarely found people who actually need lookarounds and other advanced topics and those are usually covered one-on-one as they need.

But this is fairly minor quibbling and is just rearrangement of what's here. I think this is overall a nice clear explanation. Regex syntax is honestly pretty simple once the syntax magic is explained.

What I think would be really helpful is a tool where somebody can type in a regex, have it checked for syntax and then generate the list of strings that would match it (within the constraints of limits on infinite repetition operators, like turning * to {0,2} or something.

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#57

I love this! I love RegEx but have struggled trying to “teach” others over the years. In addition to books like this, I often find writing RegEx with something like Expressions[1] (and I know there are many great website solutions, Expressions is just a great app that I find very approachable to newcomers) is a great way to learn. When you can see what you’re writing select what you want, you get a great grasp of how…

Mac only apps should die.

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#58
post #48

I personally had the most trouble with regexes because I didn't have a good mental model of how they worked. The hard part wasn't finding the correct symbol/character class I was trying to match, but coming to grips with repetition, greedy/nongreedy, etc. I took a compilers class in college where one of the projects was to implement a simple regex matcher using NFAs. Bashing my head against this for a week really hel…

I think it is understanding the algorithm that does it.

I also recommend that people learn how to read a regex by writing a small recursive program to match specific regexes. After you look at a regex and think about how it might work, intuition follows.

Actually writing the bit that turns the regex expression into said program isn't as important though. Doing that by hand 5 times is enough IMO.

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#59
Nice intro. Tangential question. Is there a regex tool that shows where the expression failed ? Not in syntax, but the logical failure point? Would be useful for when an expression gets a little long and nested and modifications need to be made.

Edit: I mean like:

Target text is abcde

Regex is /abe/

Is there a tool that will tell me it matched a and b and then failed trying to match e ?

Those sites are great resources but they are showing pass/fail and do show an excellent breakdown when something satisfies the expression, but I’m just wondering if there is something that shows partial matching until the failure point?

Re: Show HN: RegEx for Regular Folk – A visual, example-based introduction

#60
post #59

Nice intro. Tangential question. Is there a regex tool that shows where the expression failed ? Not in syntax, but the logical failure point? Would be useful for when an expression gets a little long and nested and modifications need to be made. Edit: I mean like: Target text is abcde Regex is /abe/ Is there a tool that will tell me it matched a and b and then failed trying to match e ? Those sites are great resource…

https://regexr.com/

https://regex101.com/

These websites have saved me hours of time at this point.

Post reply on HN