We currently have Cube testing surveys open for Marvel's Spider-Man and Marvel's Spider-Man Eternal. Let us know what cards you’re testing from these sets!

Tools

Regular Expressions: Powerful Magic Card Searching

September 10th, 2020 — Anthony Mattox

It’s a delightful, but also a daunting challenge to search all 27,000+ cards for effects that make sense in your deck or Cube. Lucky for us we have the incredible search tool Scryfall to help us plumb the arcane depths of Magic history.

Existing deck and Cube lists are an invaluable resource when designing your own, but can’t cover all of what Magic has to offer. You might know a lot of counterspells but what if you want to consider the complete set of options? A simple text search can put every counter spell printed at our fingertips.

Counterspell
Counter target spell

Or maybe not quite. Searching for the exact Oracle text of “counter target spell” fails to find all the cards that target a specific kind of spell.

Fortunately Scryfall offers us the ultimate tool! Regular Expressions!

Counterspell
Negate
Remove Soul
Blue Elemental Blast
Counter target ... spell

Regular Expressions are a powerful search tool used in a computing and text analysis, and perfect for searching Magic cards. In addition to searching for exact text matches, regular expressions offer tools for more sophisticated searches including leaving “blanks” where any text can appear or when text isn’t followed by other term.

Even experienced computer programmers can be intimidated by “Regexes”. They can be very complex, but the simplest tools will get you most of the value. I don’t want to perpetuate the myth that they are dark magic, but they do make you feel a bit like a wizard.

Enabling RegExes on Scryfall

To unlock these search tools on Scryfall, replace the quotes around your search terms with slashes. Instead of o:"counter target spell", enter o:/counter target spell/. Easy.

Regular Expression Tools

If you try the two above searches you’ll get the exact same results. Letter and number characters are not treated any differently. Switching our search to regex doesn’t take anything away. Simple text will work as you expect, but we’ve unlocked more tools.

Special characters like ., \, /, *, {, }, (, ), -, ^, and $ have new meanings that let us craft nuanced searches.

Wildcards

The most common tool is a wildcard. Wildcards ‘match’ any arbitrary text:

If you’re looking for effects that have a regular structure, but might have intervening terms or conditions, inserting a wildcard will broaden your search.

This search wont only return cards with the exact text “counter target spell”, but also cards that include other text in place of the wildcard, such as “creature”, “noncreature”, or “blue”.

You can read it like: find all cards that include the text “counter target”, followed by any text, followed by “spell”. In the results on Scryfall you’ll still find classic Counterspell along with many others. “No text” is allowed by .* just as well as the additional words.

A common pattern in Magic is “blink” or “flicker” effects which exile a permanent and then return it to the battlefield. We can easily find all cards which use these two words.

This gets us pretty far, but also finds more cards that use these words in other ways that don’t apply to what we’re looking for, or use each in two separate abilities. We can be more clear about what we’re looking for with a wildcard.

This ensures both that the words are in the right order and in a single ability, cutting the results in half and getting us much closer to the cards we’re actually looking for.

Aetherling
Deadeye Navigator
Conjurer's Closet
Estrid's Invocation
Ghostly Flicker
Teferi's Time Twist
exile ... return

Using .* together is so ubiquitous it’s almost synonymous with “regular expression”. On its own, a period means “any character”. An asterisk means “any number of the previous thing”. Together they search for any amount of anything.

If we want to be really nitpicky, a period allows anything except a line break, so one.*two will find cards with “one” and “two” in a single ability. For the most part this is useful anyway since the order of abilities is usually not mechanically meaningful.

Cost and Effect

Wildcards are useful for finding cards with abilities with specific costs and effects.

This search finds cards that include sacrificing something and drawing a card. You’ll also find it still returns many cards you’re not interested in.

Adding the colon ensures “sacrifice” is actually part of the cost of an activated ability. The colon isn’t any fancy regex syntax, it’s just how activated abilities are worded. Another wildcard allows other parts to the cost.

This or That

Multiple words often have similar functions. If we’re looking removal spells we might not care if it destroys or exiles its target. We can allow either by wrapping all options in parentheses and separating them with pipes.

We can include any number of options. This is similar to a wildcard in that our search is more flexible and will find multiple variations, but it’s more narrow than allowing literally any text.

Line Beginnings and Endings

^ represents the beginning of a line of text. This is particularly useful if we want to be sure an effect starts with a particular cost and no other.

This search returns all cards with an effect that costs only sacrificing a creature with no other cost. ^ ensures this is the beginning of an effect and no cost comes before.

The end of a line is represented by $.

Using the two together, this query finds all the commander creatures with the ‘partner’ ability, excluding creatures that have a specific “partner with…” or mention partner in other rules text.

Repetition

We can search for the same pattern of text repeated multiple times. This is especially useful in the mana cost of cards.

Wrapping text in parentheses (in this case the red mana symbol) makes it a “pattern” we can apply other modifiers to. + searches for that pattern 1 or more times. Just mana:/({r})+/ (without ^ and $) would find all cards that include one or more red mana symbol in the cost. That’s not especially useful on it’s own since we can already search by color. But, if we add in the line start and end characters, we can find all cards with mana costs that only include red symbols. This is useful if you’re looking for, or maybe want to exclude, high devotion cards.

We can replace + (1 or more) with options to find different counts. * means 0 or more. In this case it would also find cards with no mana at all. Replacing it with {3} finds exactly three repetitions of the pattern and {3,5} finds any repetition between, and including, 3 and 5.

Finding Special Characters

You may have noticed a problem. What happens if the text we’re searching for includes some of the characters that have special meaning in regular expressions? This will come up a lot when searching for cards with symbols, which are written with curly braces in the Oracle text. {t} represents the tap symbol, {r} red mana, and {3} three generic mana. But we know {3} means “repeated 3 times” preventing us from searching for generic mana symbols. To fix this, add a slash \ before characters you want to be treated as literal parts of the text you’re searching for (called “escaping”) rather than their special regex meaning.

This search gives us all cards with effects that cost exactly 3 generic mana, with no other cost.

What happens if you’re trying to search for an actual slash? Escape it with a slash! How far does this go? It’s slashes all the way down.

I’ll admit, introducing escaped characters is where things can start looking really funky and can be difficult to read. If you’re not sure if you need to escape a certain character, try the simple thing first and add slashes if it doesn’t give you the results you expect. Experiment! You (probably) won’t break anything.

Numbers

Special characters can be escaped with a \, but some letters preceded by a slash unlock new special abilities. \d, for example, means a number character. We could use it to find all cards that deal any amount of damage.

That’s a lot of damage. Shouldn’t Aetherflux Reservoir be there though?

\d doesn’t mean a “number” in the abstract sense. It means specifically one digit, 0 through 9 (which could also be written (0|1|2|3|4|5|6|7|8|9)). Repetition comes in handy here again. Adding a plus sign fixes this, finding numbers with one or many digits.

Numbers come up a when modifying power and toughness.

It takes a lot of slashes to escape the both the plus signs and slashes that will appear in the literal card text, but this finds all effects that increase power and toughness.1

Word Boundaries

Occasionally a particular word will also be a fragment of a longer word. A search for artifact will give you cards referencing nonartifact. Literally the opposite of what you want.

Specify something isn’t part of a longer word with “word boundaries”, represented by \b. This doesn’t match a specific character but the end or beginning of a word: where a letter character is adjacent to a space, punctuation, or is at the beginning or end of a line.

This gives us all cards referencing “artifact” excluding “nonartifact”. Note without a second \b at the end, it will still match “artifacts”.

Without regular expressions, we might try to solve this by explicitly excluding “nonartifact” with o:artifact -o:nonartifact. This almost works but will exclude cards that reference both “artifact” and “nonartifact” which we might actually care about.

Acronmys

It’s not uncommon for Magic players to abbreviate card names with acronyms (for example DRC, JTMS, or OUAT). If you come across an unfamiliar one, word boundaries, combined with wildcards, come in handy. We can use a them to search for characters appearing at the beginning of words in a particular order.

Search for card acronyms with regex
\bJ.*\bT.*\bM.*\bS

To match even more strictly, we can anchor the search to the beginning and end of the text, replace . (any character) with more specific character sets in words to exclude additional words with spaces between them and also account for the // in double face card names and ignore the back face.

Looking Back, and Forward

Some cases can’t be solved by a word boundary, but we can explicitly search for text that isn’t preceded by some other with “negative lookarounds”. Wrap the text you don’t want in (?<!text) or (?!text). Replacing “text” with the word you don’t want to be present.

We can fix our ‘nonartifact’ issue more explicitly with a negative lookbehind, specifically excluding cards that include “artifact” directly preceded by “non”.

This does the same thing looking forward finding effects that exile, but do not “return”. Notice we’ve had to throw our new best friend the wildcard .* too. Because the lookahead checks the adjacent text we need to say there could be more text between “creature” and “return”.

What’s with this weird syntax? Why (?<!)? Truthfully, no reason. A programmer decades ago had to choose something, and that’s what made sense to them. Don’t try to memorize the syntax. Get a sense of what’s possible. When you need a specific tool, look it up, or search the web.

Combining Tools

As valuable as these tools are in isolation, they’re even more so in combination as we’ve already started to see.

We can combine our search for cards that destroy or exile a creature with a wildcard to allow intervening conditions to return even more removal spells.

And, we can combine these regular expression searches of the full card text with other card attributes, like restricting our search to just instants that I can play in my blue-red commander deck.

Writing a Regular Expression

If you’re writing your own, there’s a straightforward strategy. Start simple, even with just a plain-text search, and iterate. Look at the text of cards you know fit what you’re looking for to find the right wording to use. You can try adding wildcards to broaden your search if you suspect possible matches are being missed. If you’re getting results that aren’t relevant to you, narrow your search to exclude them. As you get more comfortable writing searches you’ll be able to iterate to a practical list of results you can search through more and more quickly.

If you’re not sure where to start check out the examples below for one you can use as a template.

The Nature of Magic Cards

Critically, be aware the text of Magic cards follows specific patterns and use a specific set of terms. The consistent templating makes using regular expressions very effective. Since the same effects are worded in the same way it’s possible to write regular expression ‘patterns’ that express that templating.

Keep in mind universal structures like a colon in activated abilities, and “whenever” or “at” in triggered abilities, the way symbols are written in the Oracle text, and that ”~” can be used to reference a card’s name.

Double faced cards come with their own set of challengs. When searching within card names or Oracle text, Scryfall separates the text of the two faces with //. You can use this in searches to limit searches to only the front or back face of the card.

Wizards of the Coast has even done us a huge favor updating the “Oracle text” of old cards to match the current standards making our searches effective across Magic history.

Caveats

For many situations there won’t be a practical search that will find 100% of the cards that that might be relevant to you, or exclude all that don’t. Magic cards are written in a very consistent way, but there are a huge variety of effects. Sometimes the same result is accomplished in different ways. Your powerful new search for all counter spells still might miss a few that take an entirely different approach.

Time Stop
Ertai's Meddling
Ashiok's Erasure
Unsubstantiate
Are you my Counterspell?

This is a powerful tool, but just one of many. The perfect regex search can’t completely replace broad keyword searches and a lot of time, our communal knowledge, or just taking the time to explore your collection.


Quick Reference & Templates

Cards that reference “graveyard” and then “library” in an effect in that order:
Cards that reference “black creature” excluding those that only reference “nonblack creature”:
Creatures with “partner” but not “partner with”:
Cards that create tokens when they enter the battlefield:
Triggered abilities that create tokens:
Generic mana cost reduction effects:
“Fetch lands” in the broadest sense:
All Cards with “Cycling 1“:
Many cards that remove creatures:
Cards that remove creatures, excluding 'blink' effects that then 'return' the card to play:
Cards that deal specific, double digit amounts of damage:
Activated abilities that grant certain keywords:
Activated abilities that cost discarding a card:
Cards with flashback, excluding some that reference flashback:
Activated abilities that cost only sacrificing a creature:
Activated abilities that only cost sacrificing this card:
Activated abilities that remove a +1/+1 counter in the cost:
Include a blue mana symbol:
Include a blue mana symbol in an activated abilities cost:
...cost only a blue mana:
...cost any amount of blue but no other cost:
...cost a blue, no other mana cost, but may have other costs:
The “swords of x and y” cycle, excluding other swords:
Alternate win, and loss, conditions:
Cards that roll dice and care about dice rolling:
Land cycling cards:
Cards with names roughly fitting the acronym 'GSZ':
Cards with names exactly fitting the acronym 'GSZ':
Cards that cost only red and no generic mana:

Beyond Scryfall

Regular expressions been used for decades in computer programming. They are a powerful search tool, and also can be used to analyze, validate, and even transform text. If you find them useful, keep an eye out. You might find other places they can be used.

Thirst for Knowledge
Obsessive Search
Frantic Inventory
Frantic Search
Compulsive Research
Scour All Possibilities

More Resources

This article was updated 2023-07-23 to include some new features of Scryfall, new card types, more examples, and the acronym widget.


  1. Scryfall also includes some custom regular expression features specifically to make working with Magic text easier. \spt will match a “X/X” power toughness combination and \spp will match “+X/+X” letting you cut down on some of the difficult to manage escaping like \+\d\/\+\d/. See the full list of shorthands on Scryfall’s documentation. These extensions will only work on Scryfall and not other places you might use regular expressions.

Lucky Paper Newsletter

Our infrequent, text-only newsletter is a friendly way to stay up-to-date with what we’re doing at Lucky Paper. See past newsletters

Obsessive Search — Jim Nelson