Regex Reference and Notes

Significantly revised in summer 2025 to reflect 10+ years of experience and remove the PHP flavor of the original since I don’t use PHP regularly any more. Also trying to make it a bit more readable for people who don’t live in my very niche brain.

Character Class and Shorthands

Use these for really broad matching – any letter, any digit, etc. Combine with quantifiers to match patterns, e.g. \d{2}/\d{2}/\d{4} matches two digits/two digits/four digits, e.g. 05/13/1985 (although it does not capture any of these to return, that would require parentheses, see Grouping below).

And remember, if you’re trying to match a specific character (like the / in the date) or a word, just type it out the way you normally would in a search. cat matches “cat”

When it is not quantified, regex looks for one thing per character class. Note that how this works may depend on your text editor. For example, searching a string for [^a] may result in 100 individual matches, each of one character, whereas searching for [^a]* may result in 9 matches of long strings of numbers, spaces, and letters that are not “a.” In other text editors or languages, the same thing might only match the first (matching) character of each line. Or the first (matching) character of a whole file.

Normal characters

  • . — any single character of any kind including letters, numbers, spaces and symbols
  • \w — a single “word” character: a-z, A-Z, 0-9, and _
    • Inverse is \W which matches everything else
  • \d — a single number 0-9.
    • Inverse is \D which matches everything else

Spaces and Linebreaks

  • \s — a single whitespace (spaces, tabs, line breaks, etc)
    • Inverse is \S , matches everything else
  • \t — a single tab, useful for working in tab-separated documents
  • \r or \n or \r\n — match a single “return,” “new line,” or a combination of the two. Depending on how your file was created and the editor you’re using you may need to use one or both

Anchors

These characters let you “anchor” a search to one end or the other of a line. They don’t match any actual characters. You should leave them outside of capturing parentheses.

  • ^ — anchors to the beginning of a line, doesn’t match any character
    • ^\d{2}/ will match “05/” at the very beginning of a line but not the 13/ that might follow it
  • $ — anchors to end of the line, doesn’t match any character
    • json$ with match “json” only at the very end of a line.

Quantifiers

Use these to modify a character class search and turn it from looking for a single character into some number of characters:

  • ? — find zero or one of the thing it’s modifying
    • useful when something may or may not exist, e.g. you’re trying to match both http and https: https?.
  • * — find zero or more of the thing it’s modifying
    • useful when you’re really flexible about whether a thing exists and how many times it exists, for example if you’re matching columns in a CSV that may or may not contain anything ,(.*), might match a full column or the ,, of a blank column
  • + — find one or more of the thing it’s modifing
    • useful when you know there’s at least one. For example, replace \n+ with \n to clean up a file which has a bunch of line breaks and you just want a single linebreak between each piece of data.
  • {n} — match a specific number of occurrences. “n” stands for a number.
    • \w{5}-\d{3} would match an identifier that follows the format: WEBST-391.
  • {n1,n2} — match anything in a range of occurrences from n1 to n2.
    • Suppose that you’ve got a course catalog where course have 3-5 letters and 3 numbers. You’d use \w{3,5}-\d{3} to match both COM-242 and WEBST-391.

Very Specific Character Classes

When you’re trying to match only specific characters in some combination, use [] to designate what you’re looking for. Just like the shorthands, character classes can be combined with anchors and quantifiers to make complicated statements. Letters in character classes are case sensitive.

A simple example of why you’d use a character class is if you wanted to find all examples of “gray” and “grey” in your book and replace with “gray”. Match gr[ae]y. As noted above, it’s looking for only one of these characters, so it would not match “graey”.

While ^ at the front of a search is an anchor, a ^ inside a [] is a negative, meaning “everything except” (yes, this is annoying).

Examples:

  • [aqT,] will match the first of any of those 4 characters (the comma is a character here) you hit. It will not match uppercase AQ or lowercase t because it is case-sensitive.
  • [A-Z] will match the first capital letter you hit. See below for more on ranges.
  • [^a] match the first of anything except an “a”.
  • [^abc] will match the first character that is anything except a, b, c.

Ranges

You can use ranges of letters or numbers in character classes, so you can also write the last one as: [^a-c]. Ranges follow alphabetical a-z, A-Z and numerical 0-9 order.

  • [a-c4z6-8] matches first instance of a, b, c, 4, z, 6, 7, 8

Using Quantifiers with Character Classes

It may feel a little different but it’s the same as if you were doing the above.

  • [^a]* would match everything until it hit its first lowercase A.
  • [^A-Z]* would match everything until it hits its first capital letter of any kind.
  • In the course catalog example above, if you know the course number should always be capital letters followed by numbers, you could use [A-Z]{3,5}-\d{3} to be very specific. You might notice rows which weren’t matched because they have lowercase letters or a typo like C0M instead of COM.

Find and Replace

Ordinary regex searching finds things, but sometimes you want to replace the things you’ve found using all or part of what you found.

Remember that anything in your find must be included in your replace if you want it to show up, even if it’s not something you’re focusing on.

Groups and Backreferences

Use () to identify a group and use a “backreference” ($1 or \1 depending on the language or application you’re using) to return it. You can think of it as setting a variable with the parentheses and returning the variable with the backreference.

In the date example: \d{2}/\d{2}/\d{4}

if we add (): (\d{2})/(\d{2})/(\d{4}) we can now refer to three different groupings, the first match of 2 digits before the slash, the second match of 2 digits after the slash, and the third match of 4 digits at the end.

Backreferences

Backreferences are like calling a variable in a programming script. They are numbered in the order in which they appear in your regex command. So if: (\d{2})/(\d{2})/(\d{4}) matched 05/13/1985, then

  • $1 = 05
  • $2 = 13
  • $3 = 1985

You can reference them in you script or use the “Replace” box in a text editor or similar program that accepts regex to turn them into something like an ISO date, e.g.: $3-$1-$2 would return 1985-05-13. Note that I did not capture the / in the original and I added my own - in the replace statement.

Subpatterns

Besides doing multiple () in a row, you can nest () and refer to them separately.

For example: (([A-Z]{3,5})-\d{3})

  • The outer () select the whole course name: $1 = COM-242
  • The second () selects just the COM part, $2 = COM. We might want if we’re going to create a new column with just the discipline value. This is exactly the kind of thing Regex is useful for!

Advanced Groupings and Subpatterns

When writing groupings, you’ll want to think about how you’d want to use the data. For example, maybe you don’t care about the COM part but you are looking for all 200 level courses. In that case, you’d want to write: ([A-Z]{3,5}-(\d{1})\d{2})

\d{1}\d{2} will match the 3 numbers, just like \d{3} will, but this allows you to put a parentheses around just the first number (\d{1})\d{2} and then return it in your return.

Match: ([A-Z]{3,5}-(\d{1})\d{2})

Replace: $200: $1 (the 00 are literal, the $2 references the sub-grouping) run on

COM-242
WEBST-391

returns

200: COM-242
300: WEBST-391

Using Groups with Quantifiers and as “OR” statements.

Grouping are also used for “OR” statements!

[aqT,] will only match ONE of those characters. So you can’t use [cat] to match “cat”. Normally, you would just use the letters cat like a normal search. But if you wanted to match “cat” OR “dog”, you could search (cat|dog) where the | represents the OR. You can do as many as you want, e.g. (cat|dog|horse|fish).

You can apply quantifiers to these, but it gets really complicated really quickly.

Context Notes

Don’t forget if you’re using a language where regex needs to be wrapped in something like / /. And don’t forget that sometimes you can or need to use flags, e.g. g to match across a whole file.

MarcEdit Regex Notes

Some notes on how Regex works in MarcEdit, based on Terry’s quickref. This is shorthand which only really makes sense if you’re already familiar with Regex:

Terry’s quickref:

  1. Character escapes: \d\r\n\$\x##
  2. Character classes: [] & [^]
  3. Grouping elements: ()
  4. Anchors: ^$
  5. Quantifiers: *?+{#}
  6. Substitutions: $#
  • Match all of a hundreds/tens group: Instead of 1xx, use 1\d{2}.
  • Field names: =500.{4} catches field name, spaces, and indicators
  • Field names with contents:=041.{4}\$a\w{3}$: 041 field with only a single language code in it
  • Handling $: [^$]* - searches for anything which is NOT a $. aka strings grabs up to the $. You don’t have to escape the $.
  • Backreferences: $ not \
  • Match all captured: $0 - matches whole set of captured things.
  • Additionally: lcase() & ucase() - for matching [A-Z] or `[a-z]`` … \W to match non-word values
    • For example, ([A-Z])([A-Z\W]*) matches anything capitalized followed by all caps/non-word characters. Then replace as $1lcase($2) so first letter stays capitalized.

Derived from Microsoft Regex - you can name a sub expression

  • (?<name> subexpression )
  • (?<double>\w)\k<double>
  • \k<name> to reference.