DEV Community

Cover image for What are some Regular Expression concepts you find hard to grok?

What are some Regular Expression concepts you find hard to grok?

Nick Taylor on July 03, 2022

I was reading a Tweet from my friend Amy on Twitter where they put out a Twitter poll that is currently going on. // Detect dark theme var ...
Collapse
 
ajcwebdev profile image
ajcwebdev

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.

Collapse
 
pinotattari profile image
Riccardo Bernardini

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.

Collapse
 
ajcwebdev profile image
ajcwebdev

This is awesome, thank you for sharing!

Collapse
 
imthedeveloper profile image
ImTheDeveloper

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

Collapse
 
lexlohr profile image
Alex Lohr

That they can present security issues: owasp.org/www-community/attacks/Re...

Collapse
 
eljayadobe profile image
Eljay-Adobe

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.

Collapse
 
nickytonline profile image
Nick Taylor

Yeah, I have that book. It's really great!

Collapse
 
jayay profile image
Jakob

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 πŸ€”

Collapse
 
nickytonline profile image
Nick Taylor

Definitely write a post about it! 😎

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

This is the most useful thing I ever downloaded:
Regex cheatsheet

Collapse
 
cerchie profile image
Lucia Cerchie

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.

Collapse
 
loudacris115 profile image
Loudacris115

what?

Collapse
 
cerchie profile image
Lucia Cerchie