Wildcard Regular Expression: How the Dot, .* and Related Patterns Match Text
How wildcards work in regular expressions: the dot, .*, .+, lazy matching, escaping, glob conversion, Unicode pitfalls, and how to test a pattern safely.
A wildcard in a regular expression is a pattern element that stands for "any character" instead of one specific character. In most regex flavors, the wildcard is the dot (.), and it is usually combined with a quantifier such as * or + to match a run of unknown characters. This guide explains what the wildcard does to your text, how it differs from the asterisk wildcard used in file searches, and how to build and check a pattern step by step so that it matches only what you intend.
A note on scope: no documentation for a specific regex testing tool was supplied for this article. The behavior described here follows widely documented rules shared by common regex engines such as those in JavaScript, Python, Java and PCRE-based tools. Details differ between engines, and those differences are pointed out where they matter. Before relying on a pattern, check your tool's documentation for its regex flavor.
What a regex wildcard does to your text
The wildcard does not change your text. It is part of a search pattern. The input is the text you are searching, and the output is a set of matches: the positions and contents of the text segments that fit the pattern. The text changes only if you use the pattern in a replace operation, where each match is substituted with replacement text.
- . matches exactly one character, of any kind, except a line break by default.
- .* matches zero or more characters. It can match an empty string.
- .+ matches one or more characters. At least one character is required.
- .? matches zero or one character.
- .{3} matches exactly three characters. .{2,5} matches between two and five.
The quantifiers *, +, ? and {n,m} always apply to the element directly before them. On its own, * is not a wildcard in regex syntax. It means "repeat the previous element." This is the most common source of confusion for people coming from file searches or spreadsheet filters.
Regex wildcards vs. glob wildcards
File managers, shells and many search boxes use glob patterns, where * means "any sequence of characters" and ? means "any single character." Regular expressions use different symbols for the same ideas. If you paste a glob pattern into a regex field, it will usually match something other than what you expect, or it will be rejected as invalid.
| Intent | Glob pattern | Regex equivalent |
| Any sequence of characters | * | .* |
| Exactly one character | ? | . |
| A literal dot | . | . |
| Whole name must match | (implicit) | ^ at the start and $ at the end |
| Files ending in .txt | .txt | ^..txt$ |
Two differences cause most conversion errors. First, a dot in a glob is an ordinary dot, but in regex it is the wildcard, so a literal dot must be escaped as . to match only a period. Second, a glob normally has to match the whole name, while a regex search finds a match anywhere inside the text unless you anchor it with ^ (start) and $ (end).
Example of the anchoring difference, using standard regex rules. The pattern ..txt with no anchors will find a match inside the line notes.txt.bak, because the substring notes.txt fits. The anchored pattern ^..txt$ will not match that line, because the text after .txt prevents the $ anchor from matching.
If you work in Python, the standard library function fnmatch.translate converts a glob pattern into a regex string. The exact form of its output varies between Python versions, so inspect the result rather than assuming a particular format.
Greedy and lazy matching
By default, .* is greedy: it takes as many characters as it can while still allowing the rest of the pattern to match. This often captures more text than intended. Adding ? after the quantifier makes it lazy, so it takes as few characters as possible.
Example text:
<b>bold</b> and <i>italic</i>
Greedy pattern: <.*>
Expected match: <b>bold</b> and <i>italic</i> (one match, the whole line)
Lazy pattern: <.*?>
Expected matches: <b> </b> <i> </i> (four separate matches)
These results follow from standard greedy and lazy matching rules. They are illustrations, not output captured from a specific tool, so confirm them in your tester.
A negated character class is often more precise than a lazy wildcard. The class [^>]* means "any characters except >," so the pattern <[^>]> stops at the first closing bracket without depending on laziness. The same technique works for quoted strings: "[^"]" matches one double-quoted segment and will not run past its closing quote.
Line breaks and the dot-all flag
In most engines, the dot does not match a line break unless you turn on a flag, so a pattern like START.*END will not find a match when START and END are on different lines. The flag has different names in different engines: s in JavaScript (supported since ES2018) and PCRE-style tools, re.DOTALL or re.S in Python, and Pattern.DOTALL in Java. Some engines also treat characters other than \n as line terminators. JavaScript, for example, excludes \r and the Unicode line and paragraph separators U+2028 and U+2029 from the dot by default.
If your tool has no dot-all option, the class [\s\S] matches any character, including line breaks, because it combines whitespace and non-whitespace characters.
Do not confuse dot-all with the multiline flag (m). Multiline changes what ^ and $ mean, so they match at the start and end of each line instead of only at the start and end of the whole text. It does not affect the dot.
Unicode: what counts as one character
The dot matches one unit of text, but engines disagree on what a unit is. This matters with emoji, accented letters and other non-ASCII text.
- JavaScript without the u (or v) flag: the dot matches one UTF-16 code unit. An emoji such as 😀 (U+1F600) is stored as two code units, so ^.$ does not match it. With the u flag, the dot matches the whole code point.
- Python 3 str patterns: the dot matches one Unicode code point.
- Combining sequences in any engine: é can be stored as a single code point (U+00E9) or as e followed by the combining acute accent U+0301. In the second form, it is two code points and needs two dots to match. The two forms look identical on screen.
- Invisible characters such as a zero-width space (U+200B) or a non-breaking space (U+00A0) are matched by the dot like any other character. They can make a match one character longer than it appears.
If the character count matters, for example with a pattern like ^.{8}$ that validates length, test it with sample text that contains the non-ASCII characters you expect. Consider normalizing the text first if your tool offers that option.
Step by step: building and checking a wildcard pattern
- 1. Write down, in plain words, what the variable part may contain and what it must not contain. For example: "any characters, but not a line break and not a closing quote."
- 2. Choose the narrowest wildcard that fits. Prefer a specific class (\d, [A-Za-z], [^"]) over a bare dot when you know the allowed characters.
- 3. Pick the quantifier: * if the part may be empty, + if it must contain at least one character, {n,m} for a bounded length.
- 4. Escape every literal special character: . * + ? ( ) [ ] { } ^ $ | and \ each need a backslash when you mean the character itself.
- 5. Decide on anchoring. Add ^ and $ if the pattern must match the entire line or the entire string.
- 6. Set the flags deliberately: dot-all if the match spans lines, multiline if ^ and $ should work per line, case-insensitive if needed, and Unicode mode where your engine requires it.
- 7. Paste sample text that includes both lines that should match and lines that should not. Include edge cases such as an empty value, the shortest valid value, text with line breaks, and text with non-ASCII characters.
- 8. Inspect every highlighted match, including its start and end. Too much text usually means a greedy wildcard or a missing anchor. A missing match usually means an unescaped special character, a missing flag, or a line break.
Using wildcards in search and replace
To keep part of the matched text in a replacement, wrap the wildcard in parentheses to create a capture group, then refer to the group in the replacement field. The syntax for that reference depends on the tool: many use $1, and others use \1.
Example: swap "Last, First" into "First Last"
Pattern: ^([^,]+), (.+)$
Replacement: $2 $1 (or \2 \1, depending on the tool)
Input line: Lovelace, Ada
Intended: Ada Lovelace
The example uses [^,]+ for the first group rather than .+ so that a greedy wildcard cannot swallow the comma. It also uses a regular space after the comma. If your data contains tabs or non-breaking spaces at that position, the pattern will not match those lines. Always review the replace preview, or run the operation on a copy, before applying it to the original text.
Common mistakes
- Using * alone as a wildcard. In regex syntax, * repeats the previous element. Write .* instead.
- Forgetting to escape a dot. In the pattern file.txt, the dot also matches fileXtxt. Write file.txt.
- Leaving a pattern unanchored when the whole value must match, which lets it match inside longer text.
- Using greedy .* between two delimiters that appear more than once on a line.
- Nesting quantified wildcards, as in (.) or (.+)+. Many backtracking engines can take an extremely long time on such patterns when a match fails. Use a single quantifier or a more specific class.
- Assuming the dot crosses line breaks or matches a whole emoji without checking the flags.
What this guide could not confirm
No source specific to a particular regex tester was available. The flag names, replacement syntax and Unicode handling of your tool may therefore differ from the common behavior described here. Performance claims are limited to the general principle that nested quantifiers can cause excessive backtracking. No measurements are given. The examples show results expected under standard regex rules and were not captured from a specific tool.