Here are some common special characters used in grep patterns:
-
.(dot): Matches any single character.- Example:
a.bmatchesaab,acb, etc.
- Example:
-
*(asterisk): Matches zero or more occurrences of the preceding character.- Example:
ab*cmatchesac,abc,abbc, etc.
- Example:
-
?(question mark): Matches zero or one occurrence of the preceding character.- Example:
colou?rmatchescolorandcolour.
- Example:
-
[](brackets): Matches any one of the enclosed characters.- Example:
[abc]matchesa,b, orc.
- Example:
-
[^](caret inside brackets): Matches any character not enclosed.- Example:
[^abc]matches any character excepta,b, orc.
- Example:
-
{n}: Matches exactlynoccurrences of the preceding character.- Example:
a{3}matchesaaa.
- Example:
-
{n,}: Matchesnor more occurrences of the preceding character.- Example:
a{2,}matchesaa,aaa, etc.
- Example:
-
{n,m}: Matches betweennandmoccurrences of the preceding character.- Example:
a{2,4}matchesaa,aaa, oraaaa.
- Example:
-
|(pipe): Acts as a logical OR between patterns.- Example:
cat|dogmatches lines containing eithercatordog.
- Example:
-
()(parentheses): Groups patterns together.- Example:
(abc|def)matches eitherabcordef.
- Example:
These special characters enhance the flexibility of pattern matching in grep. If you want to learn more about using grep, consider exploring related labs on LabEx!
