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.
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?
It might be nice to touch on composition as a good way to get started is to test out individual pieces and be confident they work when you're putting them together.
If you're building a complex regular expression, setting smaller parts in variables and dropping them in with (?:${part}) makes things a bit more readable. It also exposes a real weakness of most regex engines. In particular, alternation is a first-class operation, but complement and intersection, while theoretically possible[1] are typically not. A person might guess that to match three keywords is /.keyword1.&.keyword2.&.keyword3./ Or maybe /.keyword1.&(.keyword2.)!/ to match keyword1 and not keyword2. But those won't work, so it's a good idea to explain some options, an obvious one being /keyword1/.test() && !/keyword2/.test() In the section on lookaround assertions, it's probably useful to note that (?=thing1)(?=thing2) can match both, and it's a good mental model for it, but that it comes with a few gotchas. [1]: https://www.researchgate.net/publication/220994310_Succinctn...
Comments (108) 46 threads
Earlier commentsOne visual enhancement that could be really helpful would be to hover over the regex or the match and see the reciprocal highlighted.
[1]: https://www.apptorium.com/expressions
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?
https://regex101.com/
These websites have saved me hours of time at this point.
http://p3rl.org/rxrx
rxrx -e'"abcde" =~ /abe/'
Demo: https://blog-cloudflare-com-assets.storage.googleapis.com/20...
http://p3rl.org/re#'debug'-mode
perl -Mre=debug -e'"abcde" =~ /abe/'
----
https://stackoverflow.com/questions/2348694/how-do-you-debug...
If you're building a complex regular expression, setting smaller parts in variables and dropping them in with (?:${part}) makes things a bit more readable.
It also exposes a real weakness of most regex engines. In particular, alternation is a first-class operation, but complement and intersection, while theoretically possible[1] are typically not.
A person might guess that to match three keywords is /.keyword1.&.keyword2.&.keyword3./
Or maybe /.keyword1.&(.keyword2.)!/ to match keyword1 and not keyword2.
But those won't work, so it's a good idea to explain some options, an obvious one being /keyword1/.test() && !/keyword2/.test()
In the section on lookaround assertions, it's probably useful to note that (?=thing1)(?=thing2) can match both, and it's a good mental model for it, but that it comes with a few gotchas.
[1]: https://www.researchgate.net/publication/220994310_Succinctn...
All comments shown.
Couldn't load more comments. Check your connection and try again.