devices.txt contains disk, disk0, disk7, disk12, and diskA.
Extract only lines whose entire value is disk followed by one digit.
Which basic regular expression is appropriate?
[[:digit:]] matches one digit. Anchoring it between ^disk and $ excludes missing, extra, or nonnumeric suffixes.
Detailed explanation
^disk.$Incorrect. . matches any single character, including A.
Incorrect. A dot accepts any character, including the letter A.
^disk[:digit:]$Incorrect. Without brackets, [:digit:] is not a character class.
Incorrect. The POSIX class needs the outer brackets in [[:digit:]].
^disk[[:digit:]]$Correct. The POSIX digit class matches exactly one digit between the anchors.
Correct. Exactly one digit is required between the two line anchors.
^disk[0-9]*$Incorrect. * permits zero or multiple digits, including disk and disk12.
Incorrect. The star permits no digits or multiple digits, so disk and disk12 also match.
Try it yourself
An example you can run in a temporary verification environment.
printf 'disk\ndisk0\ndisk7\ndisk12\ndiskA\n' | grep '^disk[[:digit:]]$'Expected result
disk0
disk7Key points
- POSIX character classes
- The class matches one character
- Anchors constrain the whole line
Notes
- Environment: GNU grep 3.x / C.UTF-8ロケール
- Use temporary files and harmless commands for pattern experiments.
- Regular-expression details should be checked against the target GNU implementation.
Foundation review
Place the condition
A single character class occurrence means exactly one digit; a repetition operator would broaden the count.
Check the result
Test disk, disk0, disk7, disk12, and diskA as separate lines.