From names.txt, select only the line api.v1.
Exclude api-v1, oldapi.v1, and api.v10.
Which basic regular expression is appropriate?
In a regular expression, . means any character. Escape it as \. or use [.] and anchor both ends for an exact line match.
Detailed explanation
^api.v1$Incorrect. The unescaped period also matches the hyphen in api-v1.
Incorrect. The unescaped period accepts the hyphen in api-v1.
^api[.]v1Incorrect. It does not anchor the end, so api.v10 can also match.
Incorrect. The period is literal, but without an end anchor api.v10 also matches its prefix.
api\.v1$Incorrect. It does not anchor the beginning, so oldapi.v1 can match.
Incorrect. Without a start anchor oldapi.v1 can match its trailing portion.
^api\.v1$Correct. The escaped period is literal and the anchors require the whole line.
Correct. The escaped period and both anchors require exactly api.v1.
Try it yourself
An example you can run in a temporary verification environment.
printf 'api.v1\napi-v1\noldapi.v1\napi.v10\n' | grep '^api\.v1$'Expected result
api.v1Key points
- . means any character
- \. matches a literal period
- Both anchors make a full-line match
Notes
- Environment: GNU grep 3.x
- 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
Literalizing the period handles its special meaning; both anchors exclude extra prefixes and suffixes.
Check the result
Include api.v1, api-v1, oldapi.v1, and api.v10 in one test input.