This content has been automatically translated from Ukrainian.
During the study of SQL, working with the terminal, setting up domains, or searching for files, you may encounter the term wildcard. It is a quite simple yet universal mechanism.
Wildcard (substitution character) is a special character that can replace one or several other characters. It allows you to work not with a specific value, but with a certain pattern.
Why is it called a wildcard?
The name wildcard comes from card games. In English, wild means "wild" or "free," and card means "card."
In card games, a wild card is a card that can take on the value of another card. The most understandable example is the joker, which, depending on the rules of the game, can replace any other card.
In programming, a similar principle is used. Instead of explicitly specifying each possible value, we use a special character that can replace them.
In fact, a wildcard is a kind of joker in the world of programming.
What does wildcard mean in SQL?
In SQL, wildcard characters are most often used with the LIKE operator to search for data by pattern.
The main wildcard characters in SQL:
- % — replaces any number of characters, including none.
- _ — replaces exactly one character.
For example, you need to find all users whose names start with John:
SELECT * FROM users WHERE name LIKE 'John%';
This SQL query may return John, Johnny, Johnson, or John Smith.
If you need to find a word anywhere in the text, the wildcard character can be placed on both sides:
SELECT * FROM articles WHERE title LIKE '%SQL%';
And if you need to replace only one character:
SELECT * FROM users WHERE name LIKE 'J_n';
This pattern can match the names Jan, Jon, or Jen.
Wildcard in Linux, macOS, and Bash
When working with the terminal, wildcard characters allow you to perform operations on a group of files at once.
For example:
ls *.jpg
This command will list files with the .jpg extension in the current directory that match the pattern.
Another example:
ls report_202?.pdf
The ? character replaces one arbitrary character. This pattern can match files report_2024.pdf, report_2025.pdf, or report_2026.pdf.
In Bash and other command shells, matching file paths with such patterns is called globbing.
What is wildcard DNS?
Wildcard characters are also used in the Domain Name System (DNS).
For example, a wildcard DNS record:
*.example.com
allows serving requests to subdomains for which no corresponding individual DNS records have been created.
These can be:
- blog.example.com
- shop.example.com
- demo.example.com
- test.example.com
This approach is especially convenient for SaaS platforms, multi-blog systems, and applications that dynamically create subdomains for users.
What is a wildcard SSL certificate?
A wildcard SSL certificate allows protecting multiple subdomains with a single certificate.
For example, a certificate for *.example.com can protect blog.example.com, api.example.com, and shop.example.com.
However, it does not cover next-level subdomains, such as api.dev.example.com. Also, the main domain example.com must be separately specified in the certificate.
Using wildcard in Gitignore
Another common example of using wildcard characters is the .gitignore file, which defines which files Git should ignore.
*.log *.tmp logs/*
Instead of listing each file individually, you can describe a general pattern for a whole group of files.
What is the difference between wildcard and regular expressions (Regex)?
Wildcards and regular expressions are used for pattern matching, but they work differently.
For example, to find files with the .jpg extension, you can use a wildcard:
*.jpg
A similar pattern for a regular expression:
.*\.jpg$
In a wildcard pattern, * usually means any sequence of characters. In regular expressions, * means repeating the preceding element zero or more times.
Regular expressions have significantly more capabilities, but wildcard patterns are simpler for everyday tasks.
Main wildcard characters
The most common substitution characters you may encounter in programming:
- * — any sequence of characters in glob patterns.
- ? — one arbitrary character in glob patterns.
- % — any sequence of characters in SQL LIKE.
- _ — one arbitrary character in SQL LIKE.
- [abc] — one of the specified characters in glob patterns.
- [0-9] — one character from the specified range in glob patterns.
It is important to understand that there is no universal wildcard syntax. The meaning of the characters depends on the programming language, tool, or system in which they are used.
Wildcard is a simple mechanism that allows reducing the amount of code, simplifying information retrieval, and automating work with a large number of objects. And if you need to remember its purpose, just think of a regular playing card joker.
Like it?React
🧵
This post doesn't have any additions from the author yet.