system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: How do regexes...work?

So, have you ever run into a programming concept that you just *couldn't* learn, no matter what?  Regexes are that for me.  I can't tell you how many times I've read up on the basics of regexes, looked in books, seen websites, used tutorials, and even copy-and-pasted regexes into my code...and they're still fucking magic to me.  For some reason, my brain just shuts down when I'm looking at them.  I think one of my profs scarred me in university or something.

I put to you a possibly impossible task: can you explain the basic principles behind regexes (how they work, why they work) in a way that I can remember and makes sense?  Possibly with a few basic examples, building on each other? 

You'd really be improving my life.

EDIT: I'm happy I posted this; hopefully other people benefit from it in the future!
gpt
(Edited to fix formatting and make a clarification.)

Regular expressions are just a way to say "match things that show up in a particular order."

Let's say car manufacturers all start painting the first letter of their brand names on the sides of their cars to make them easy to identify.  Dodges get a big "D", BMWs get a big "B", Toyotas get a big "T", etc.

Let's also say you're standing the side of a road with a flag in one hand.  Your job is to watch traffic go by and every time you see a Dodge ("D"), point at it and wave the flag.  So you sit there for awhile not seeing any Dodges and you do nothing.  Then a Dodge goes by with its big D painted on the side, so you point at it and wave your flag like a madman.  Congratulations, you've become a regular expression engine and have just evaluated your first regular expression:

 > /D/   (D.  The slashes are sometimes used to denote regular expressions.)

What you've done is called "finding a match."  Easy enough, right?  You can sit there and point at Dodges all day.  That gets kind of boring, so your overseers give you a another assignment, find Dodges followed by an Audi.  That's a more complex regular expression:

 > /DA/   (D followed by A)

So you start by looking for a Dodge, and when you find one, you look at the car that follows it.  If the second car is an Audi, you've found a match and you start pointing and waving your flag.  If not, you start over and look for a Dodge.

One more assignment, a Dodge followed by an Audi followed by a Chevrolet followed by another Chevrolet:

 > /DACC/  (D followed by A followed by a C followed by another C)

What's important to note here is that you don't start looking for an Audi until after you see a Dodge and Chevrolets aren't on the radar until after you've found the Audi.  You progress across the expression starting from the left and not waving the flag until you fall off the right edge.  If you fail somewhere in the middle (say, a Dodge followed by an Audi followed by a Ferrari), you go back to square one.

The pattern you should be seeing here is that you're being given a list of "things" to look out for.  That's really all there is to it.  Where regular expressions get complex is in defining what a "thing" is.

Individual characters are easy, but you can use certain special characters to give things special meaning, such as a dot that matches anything:

 > /DA.T/  (D, A, anything, T)

This means that Dodge, Audi, Hyundai, Toyota (DAHT) will match, as will Dodge, Audi, Dodge, Toyota (DADT) or Dodge, Audi, Mercedes, Toyota (DAMT), all because any car can fit in the blank where the dot is.  When you wave your flag and report a match, you report the sequence that matched.

Other characters, like the asterisk, plus sign and braces, apply special meaning to the thing before them to indicate how many you want:

 > /DA*T/  (D followed by zero or more As followed by a T)
 >
 > /DA?T/  (D followed by zero or one As followed by a T)
 >
 > /DA+T/  (D followed by one or more As followed by a T)
 >
 > /D{3,5}AT/ (Between 3 and 5 Ds followed by an A and a T)

Brackets make up a compound "thing" that can be any of the things inside it:

 > /D[ABM]T/  (Dodge followed by a Audi or BMW or Mercedes followed by a Toyota)

So can parentheses:

 > /(DA)?T/   (Optional D-followed-by-A followed by a T, so T or DAT will match)
 >
 > /(DA|TH)F/ (D-followed-by-A or T-followed-by-H, followed by F, so DAF or THF will match)

The pipe symbol within the parens means "any of these regular expressions is considered a match for this thing." 

Some special characters are called "anchors" that mean you find the expression at the very beginning or the very end of the string:

 > /\^DAT/  (Dodge, Audi, Toyota being the first cars you see that day)
 >
 > /DAT$/  (Dodge, Audi, Toyota being the last cars you see that day)


There's a lot of punctuation flying around a complex regular expression, but the key to understanding it is to break it into individual "things" and treat them like items on a checklist.  There are also a lot more complex things, but what I've outlined above are the basics that get used 90% of the time.

HTH, and here's looking forward to your sixth birthday.  :-)