I was reading a Tweet from my friend Amy on Twitter where they put out a Twitter poll that is currently going on.
to which I replied in my funny way.
And this got me to thinking that many folks struggle with regular expressions. What are some things that trip you up when it comes to regular expressions?
Top comments (13)
I said this on Twitter but for me the thing that trips me up is constantly having to translate in my head between the thing I want to do in a regular expression and the specific regex syntax I need to accomplish it. The regex mental model is actually quite simple but the syntax is a nightmare.
I agree. I actually like regular expressions, I find that they are a very powerful tool. However, the syntax leaves somehow to desire... It is not too bad for simple stuff (e.g., regexp for identifiers, especially with POSIX extension), but when the regexp gets complex (e.g., regexp for floating point numbers...) they look like a cat walking on the keyboard.
I tried to search for a different syntax, compact as the existing, but more readable but with no result. In the end I wrote myself a "regexp building" library that proves useful for the most complex cases.
This is awesome, thank you for sharing!
Greedy / non-greedy lookback / forward.
Standard pattern matching I'm pretty good at now with regex but capture groups combined with multiple captures and the greedy stuff is a little crazy still.
I found using regexr.com really helpful as you can visualise quickly what's going on
That they can present security issues: owasp.org/www-community/attacks/Re...
The book Mastering Regular Expressions: Powerful Techniques for Perl and Other Tools by Jeffrey E.F. Friedl really helped me grok regular expressions. Excellent book.
Language agnostic, really; it's about the regex concepts.
Yeah, I have that book. It's really great!
I made a tool that helps me with that:
sloppy-regex.netlify.app
It can generate matching strings from a regex, or build a regex that would match the provided test strings. The regex and input strings are being tested against the libpcre2 library, which I cross-compiled into a WebAssembly binary.
I think, I'm going to write a post about it ๐ค
Definitely write a post about it! ๐
This is the most useful thing I ever downloaded:
It's when it gets -- 'nested' I guess? But when there need to be multiple matches for several pieces of the pattern and that gets reflected in the syntax, I start to get confused.
what?
For example, ihateregex.io/expr/url/