Extract only color and colour from words.txt.
The u may occur zero or one time.
Which ERE is correct?
In an ERE, ? makes the preceding element optional, allowing zero or one occurrence.
Detailed explanation
^colou*r$Incorrect. * permits any number of u characters, not just zero or one.
Incorrect. * permits any number of u characters, not just zero or one.
^colou?r$Correct. u? allows color and colour.
Correct. u? allows color and colour.
^colou+r$Incorrect. + requires at least one u, so color is excluded.
Incorrect. + requires at least one u, so color is excluded.
^colo(u|r)$Incorrect. The alternation changes the word structure and does not represent optional u.
Incorrect. The alternation changes the word structure and does not represent optional u.
Try it yourself
An example you can run in a temporary verification environment.
printf '%s\n' color colour colouur colr | grep -E '^colou?r$'Expected result
color
colourKey points
- ? means zero or one
- It applies to the preceding element
- Anchors exclude extra characters
Notes
- Environment: GNU grep 3.x / LC_ALL=C
- 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.