Regular Expressions Icon

Regular Expressions

Now you have two problems.
11 Stories
All Topics

TypeScript github.com

A compiled-away, type-safe, readable RegExp alternative

This isn’t the first attempt at making regular expressions more approachable, but it’s the first one I’ve seen that also brings type safety to the table. Example code:

import { createRegExp, exactly, oneOrMore, digit } from 'magic-regexp'

// Quick-and-dirty semver
createRegExp(
  oneOrMore(digit)
    .as('major')
    .and('.')
    .and(oneOrMore(digit).as('minor'))
    .and(exactly('.').and(oneOrMore(char).as('patch')).optionally())
)
// /(?<major>(\d)+)\.(?<minor>(\d)+)(\.(?<patch>(.)+))?/

Opensource.com Icon Opensource.com

Solving Wordle with help from the Linux command line

Jim Hall used grep and some fancy regular expressions to get a leg up on Wordle, the word game that keeps you guessing… but only once per day. Some people may think of Jim’s technique as cheating. I wouldn’t necessarily disagree.

But it’s a lot better than View Source-ing to get at the answer, which you can also do in a pinch. 😉

(Also there’s zero point to Wordle other than having fun, so it’s really only cheating if your answer-finding-method is less fun than you’d have otherwise. Even then, you’re only cheating yourself.)

History whyisthisinteresting.substack.com

The history of regular expressions

Buzz Anderson lays out the history of one of the most beloved/hated tools in every developer’s tool belt:

The concept of a regular expression has a surprisingly interesting history that dates back to the optimistic, mid-20th Century heyday of artificial intelligence research.

The term itself originated with mathematician Stephen Kleene. In 1943, neuroscientist Warren McCulloch and logician Walter Pitts had just described the first mathematical model of an artificial neuron, and Kleene, who specialized in theories of computation, wanted to investigate what networks of these artificial neurons could, well, theoretically compute.

Learn rexegg.com

The best regex trick

This post does a great job of laying out all of the cumbersome/verbose ways you can solve a problem with regular expressions and then showing the tricky way of doing the same thing without all the hassle. With this trick up your sleeve, you’ll be able to answer all of these questions:

  • How do I match a word unless it’s surrounded by quotes?
  • How do I match xyz except in contexts a, b or c?
  • How do I match every word except those on a blacklist (or other contexts)?
  • How do I ignore all content that is bolded (… and other contexts)?

JavaScript github.com

A JS library for building regular expressions in (almost) natural language

I’ve seen tools like this one before, but every time I do I still pass them around because regular expressions are powerful but also quite unapproachable. With this library, you can chain together function calls describing how you think about a rule and it’ll generate the expression for you. Example!

const SuperExpressive = require('super-expressive');

const myRegex = SuperExpressive()
  .startOfInput
  .optional.string('0x')
  .capture
    .exactly(4).anyOf
      .range('A', 'F')
      .range('a', 'f')
      .range('0', '9')
    .end()
  .end()
  .endOfInput
  .toRegex();

// Produces the following regular expression:
/^(?:0x)?([A-Fa-f0-9]{4})$/

That matches and captures the value of a 16-bit hexadecmal number like 0xC0D3. Assuming this library generates clean, somewhat optimal expressions (which might be a big assumption), it’s a great way to learn how to write your own expressions over time.

Jan Meppe janmeppe.com

Regex for noobs (like me!)

This is a great introduction to that regex magic!

This blog post is an illustrated guide to regex and aims to provide a gentle introduction for people who never have fiddled with regex, want to, but are kind of intimidated by the whole thing.

If you understand regex it suddenly becomes this super fast and powerful tool … but you first need to understand it, and honestly I find it a bit intimidating for newcomers!

JavaScript flaviocopes.com

A guide to JavaScript regular expressions

Flavio Copes:

Learn everything about JavaScript Regular Expressions with this brief guide that summarizes the most important concepts and shows them off with examples.

Regular expressions can be a developer’s best friend or worst nightmare, depending on how well you can wield them. I’ve been using them (with varying degrees of success) since the early aughts, yet I still learn something new every time I read a tutorial like Flavio’s.

Player art
  0:00 / 0:00