Using ChatGPT for Regex

Over the past few months, I’ve been experimenting a lot with AI – ChatGPT, Stable Diffusion, Midjourney, and the explosion of Chrome extensions that put AI at your fingertips. I’ve used it to create images, draft emails, and conduct research.

So far, one of the most beneficial real-world uses for me has been with writing regular expressions. I use regex primarily with Google Analytics or Looker Studio – and only sparingly. So each time I want to use regex I have to “relearn” how to write an effective (and accurate) regular expression. Enter ChatGPT.

I gave the following prompt to ChatGPT:

Create a regular expression to find all pages that begin with /part-finder-single/ and contain lookup=vin

Within about 2 seconds, ChatGPT responded not only with the regular expression I needed (see below), but also an explanation of the components used with examples of strings that would match as well as strings that would not match. Take a look.

Sure, here’s a regular expression that should match all pages that start with “/part-finder-single/” and contain “lookup=vin” anywhere after it:

^/part-finder-single/.*lookup=vin.*

Here’s a breakdown of the different components of this regular expression:

  • ^ matches the beginning of the string.
  • /part-finder-single/ matches the literal characters “/part-finder-single/”.
  • .* matches zero or more of any character after the “/part-finder-single/” part.
  • lookup=vin matches the literal characters “lookup=vin”.
  • .* matches zero or more of any character after the “lookup=vin” part.

So this regular expression will match strings like:

  • /part-finder-single/?lookup=vin&make=Toyota
  • /part-finder-single/?year=2022&lookup=vin
  • /part-finder-single/?lookup=vin&model=Corolla
  • /part-finder-single/abc123?lookup=vin&make=Honda

but it won’t match strings that don’t start with “/part-finder-single/” or don’t contain “lookup=vin”, such as:

  • /part-finder-single/
  • /part-finder-single/?lookup=vehicle
  • /part-finder-single/1234567890
  • /part-finder-single/?make=Ford&model=Mustang