/now
projects
ramblings
smol projects

regex flavors

23.08.2023 1 min read

Finally getting around to learning regex proper, and I’ve been working on some problems. One was to check whether a string had at least one uppercase character AND at least one lowercase character. I tried to use a lookahead for this:

grep -e '^(?=.*[a-z])(?=.*[A-Z])[a-zA-Z]{2,}$' at-least-one-upper-one-lower.md

Ran it on RegExr, which worked. However, this wasn’t returning anything on Ubuntu. So I did some digging, and found this stackoverflow answer (seriously, how did anyone get anything done before S/O):

Extended regular expression doesn’t have positive look-ahead feature. See the regex flavor comparison

To get it to work, you have to run grep with Perl-flavored regex, which you can do with the -P flag:

grep -P '^(?=.*[a-z])(?=.*[A-Z])[a-zA-Z]{2,}$' at-least-one-upper-one-lower.md

Sidenote: regex is hard. But it’s quite fun when you get something that works

Built with Astro and Tailwind 🚀