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 also matches the hyphen in api-v1.
^api[.]v1Incorrect. It does not anchor the end, so api.v10 can also match.
Incorrect. It does not anchor the end, so api.v10 can also match.
api\.v1$Incorrect. It does not anchor the beginning, so oldapi.v1 can match.
Incorrect. It does not anchor the beginning, so oldapi.v1 can match.
^api\.v1$Correct. The escaped period is literal and the anchors require the whole line.
Correct. The escaped period is literal and the anchors require the whole line.
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
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.