Regular Expression Quantifiers and Repetition
Its name stands for Generalized Regular Expression Parser.
From File to Matching Line
grep is a command-line program built into Unix, Linux, and macOS systems. Its name stands for Generalized Regular Expression Parser. You give grep a regular expression pattern and a filename, and it searches the file for matching lines. Instead of searching inside a string variable, as a Python search function does, grep searches through a file stored on disk.
A First grep Trace
This command has three important parts. grep is the command being run. '^From:' is the pattern. mbox-short.txt is the file being searched. The caret anchors the pattern to the beginning of a line, so the complete string From: must appear at the start of a matching line.
What do you think happens?
What happens to a line that contains the text From: in the middle rather than at its beginning?
Reveal answer
grep skips that line for the pattern ^From:.
The caret requires the pattern to begin at the start of the line. grep prints a line only when that line matches the supplied regular expression.
grep reads the file line by line. For each line, it tests the regular expression. A matching line is printed in full to the terminal; a nonmatching line is skipped. This continues until grep reaches the end of the file. The displayed result is sent to standard output, also called stdout.
Repetition Notation in Context
Quantifiers are part of the regular-expression topic named by this article, but the supplied reference material does not define the matching behavior of *, +, ?, or {n,m}. It therefore cannot support a precise explanation of how many characters or groups each symbol matches. The reliable lesson from the supplied material is that grep receives a regular-expression pattern, applies that pattern to each line, and prints lines that match.
Python and grep Syntax
| Intended match | Python notation described in the source | grep notation described in the source |
|---|---|---|
| A character that is not whitespace | \S | [^ ] |
| Meaning of the grep form | Not applicable | A character that is not a space |
The supplied material documents this specific syntax difference; it does not claim that the two tools support exactly the same regular-expression features.
Python and grep both use regular expressions, but they do not support exactly the same set of features. In Python, the source describes \S as a shorthand for any non-whitespace character, including characters that are not a space, tab, or newline. The source states that grep does not support this shorthand. For grep, the documented alternative is [^ ], a character class meaning any character that is not a space.
These two commands illustrate a syntax translation problem. The first uses the Python shorthand documented in the source, but grep does not support that shorthand. The second uses the grep character-class notation described in the source. The filename is shown as a placeholder because the source does not provide a file with sample output for this comparison.
When a Search Misses
A grep search can produce no useful output for two different reasons covered by the reference material. The pattern may be too restrictive, such as ^From:, which accepts only lines beginning with From:. Or the pattern may use syntax that belongs to Python but is not supported by grep, such as \S. Debugging starts by separating these possibilities: inspect what the pattern requires, then check whether the notation is supported by grep.
Common mistake Using ^From: when the target text is not necessarily at the beginning of a line.
What it looks likegrep '^From:' mbox-short.txtWhy it failsThe caret anchors From: to the start of the line, so a line containing From: elsewhere does not match this pattern.
Do this insteadFirst decide whether the text must begin the line. Use the anchor only when that position is part of the search requirement.
Common mistake Using Python's \S shorthand in grep.
What it looks likegrep '\S' filenameWhy it failsThe supplied material states that grep does not support the \S shorthand.
Do this insteadFor the documented non-space case, use grep's [^ ] character-class notation.
Common mistake Expecting grep to print the part of a line that matched.
What it looks likegrep '^From:' mbox-short.txtWhy it failsThe source states that grep prints the entire matching line to standard output.
Do this insteadRead the complete output line as the matching record, not only the substring that satisfied the pattern.
grep '\S' filenameThe pattern uses a Python shorthand that the source says grep does not support.
grep '[^ ]' filenameThe pattern uses the grep character-class notation described in the source.
Guided Practice
Suppose you want grep to find sender lines in mbox-short.txt. Write the command using the pattern that matches lines beginning with From:.
Hints
- The command format is grep 'pattern' filename.
- Use the caret when the required text must be at the beginning of a line.
- The source uses
mbox-short.txtas the filename.
A Python pattern uses \S to represent a non-whitespace character. Translate that intended search into the grep notation documented in the reference material.
Hints
- The source says grep does not support \S.
- Use the character-class form [^ ].
Key Takeaways
- 1grep searches files with regular-expression patterns and prints every matching line.
- 2Its basic command structure is grep 'pattern' filename.
- 3grep reads and tests a file one line at a time, skipping nonmatching lines and sending matching lines to standard output.
- 4The caret in ^From: requires From: to occur at the beginning of a line.
- 5The supplied material documents that grep does not support Python's \S shorthand; it gives [^ ] as the corresponding grep notation for a non-space character.
- 6The supplied reference does not define the matching rules for the quantifiers *, +, ?, or {n,m}, so those rules should be learned from a source that explicitly documents them.
At a glance
- grep applies a regular-expression pattern to the lines of a file.
- A grep command contains the command name, a pattern, and a filename.
- Anchors can make a pattern more restrictive, while unsupported syntax can prevent the intended search from working.
- Python and grep do not necessarily use the same regular-expression notation.
- The supplied source explains grep behavior and one syntax difference but does not define the semantics of common quantifiers.