Extracting Data with Groups and Parentheses
The * quantifier matches zero or more characters, while + matches one or more characters.
Variable-Length Content
Regular expressions become especially useful when the text between two markers is not always the same length. In an email header, for example, the text between the colon and the at-sign can be short, as in From: alice@, or longer, as in From: stephen.marquard@. A single wildcard character cannot cover both cases. Quantifiers let a pattern match a character repeatedly rather than only once.
What do you think happens?
Which pattern can match a variable-length sequence between From: and @: a single dot, .*, or .+?
Reveal answer
Answer: .+
The dot matches one character, while the plus quantifier requires one or more characters. Together, .+ matches a variable-length sequence containing at least one character.
Quantifier Choices
The * quantifier matches zero or more characters. The + quantifier matches one or more characters. This difference determines whether the quantified part is allowed to be empty.
| Pattern | Minimum characters | Meaning | Use when |
|---|---|---|---|
| .* | 0 | The dot can be matched zero or more times | An empty section is acceptable |
| .+ | 1 | The dot must be matched one or more times | At least one character is required |
The .+ Pattern
The dot wildcard matches any single character except a newline. The plus quantifier repeats that wildcard one or more times. Therefore, .+ matches one or more characters in a row. When placed between two known markers, it can cover content whose length varies from one match to another.
Matching a Header Prefix
Apply the pattern ^From:.+@ to the source text From: stephen.marquard@uct.ac.za.
Start at the line beginning: The start anchor requires the match to begin at the start of the line.
Match From:: The literal marker From: is matched next.
Expand .+: The .+ portion matches one or more characters after the colon, allowing for the variable-length text stephen.marquard.
Match @: The pattern then requires an at-sign after the variable-length text.
The pattern matches the beginning portion From: stephen.marquard@ of the source line.
Parentheses and Captured Data
When extracting data between markers, parentheses identify the variable-length portion that is being grouped for extraction. In a pattern shaped like From:(.+)@, the surrounding markers are From: and @, while the content matched by .+ is the part placed inside the group. The group keeps the changing data conceptually separate from the fixed markers.
Greedy Expansion
A quantifier does not know in advance how many characters it must consume. By default, the regex engine uses greedy matching: it expands the quantifier as far as possible while still allowing the overall pattern to succeed. For a pattern ending with @, the .+ portion keeps consuming characters until the engine can satisfy the required at-sign. If the rest of the pattern cannot succeed after an expansion, the engine can backtrack and try a shorter expansion.
This behavior explains why .+ is useful for variable-length data but also important to inspect when debugging. The quantifier is not limited to a predetermined number of characters. It keeps expanding until the surrounding pattern determines that the match can succeed or must be reconsidered.
Debugging Failed Matches
Using + when the data may be empty
The plus quantifier requires at least one character, so it cannot match an empty section.
Fix:
Use * when zero characters between the markers is acceptable.Using * when content is required
The asterisk can match zero characters, so it does not enforce the presence of data.
Fix:
Use + when valid data must contain at least one character.Assuming a wildcard matches only one character
The dot matches one character, while the variable-length text requires a quantifier.
Fix:
Combine the dot with + or * according to whether content is required.Ignoring greedy expansion
Quantifiers are greedy by default and expand as far as possible while still allowing the overall pattern to succeed.
Fix:
Trace the characters consumed by the quantifier and check the following marker or pattern requirement.
Guided Practice
A text format contains an opening colon and a closing at-sign. The section between them must contain at least one character. Decide whether the pattern should use .* or .+, then explain what would happen if the two markers appeared with nothing between them.
Hints
- Ask whether zero characters are valid.
- Remember that + requires at least one character and * permits zero characters.
Trace the pattern ^From:.+@ against From: alice@ and From: stephen.marquard@. Identify what the .+ portion must consume in each case and explain why one fixed number of wildcard characters would not be sufficient.
Hints
- The .+ portion begins after From:.
- The closing at-sign tells the overall pattern where the variable-length portion must end.
Key Takeaways
- The * quantifier matches zero or more characters, while + matches one or more.
- The .+ pattern combines the dot wildcard and plus quantifier to match variable-length content containing at least one character.
- A pattern such as ^From:.+@ uses fixed markers around a variable-length section.
- Greedy matching expands a quantifier as far as possible while still allowing the overall pattern to succeed.
- Use + when content is required and * when an empty section is acceptable.
Key Takeaways
- The * and + quantifiers differ in whether they allow zero characters.
- The .+ pattern matches one or more characters of variable length between known markers.
- Parentheses can separate the variable data portion from the surrounding markers.
- Greedy matching expands quantifiers until the surrounding pattern can succeed.
- Debug quantifier problems by checking whether content is required and by tracing where the closing marker is matched.