02. Literal and Special Characters

There are two types of characters you should look out for when reading a regular expression - literal and special characters.

Literal Characters

Literal characters are exactly what they sound like they mean - they hold the literal values of text. These include alphabet letters; for example, if I typed a regular expression testing then it would match all occurrences of the sequence t, e, s, t, i, n, g (in that order) of our query.

Using just literal characters is limiting. We want our search queries to be dynamic and expansive so that we can find more than just a literal word. For example, how can we search for all character groupings containing the YYYY/MM/DD format? This is where special characters come into play.

Special Characters

Special characters, also known as metacharacters, hold special meanings and are usually symbolized by punctuation.

The following text is a list of all the metacharacters we'll look at. We'll start with basic regular expressions (BRE), then reach out the extended group (ERE). Glance over them, and we'll go in detail within the following lessons.

Basic Regular Expressions (BRE)

Basic regular expressions are the grep default. No need to apply any options when using the following:

\
Take the literal value of the following character.
.
Match any single character, but cannot be NULL.
*
Match zero or many times the character that precedes it.
^
Anchor element used to specify "starts with" some pattern.
$
Anchor used to specify "ends with" some pattern.
[...]
Matches any one of the characters enclosed.
[^...]
Matches the inverse of characters enclosed.
\{n,m\}
Matches at least n occurrences, but at most m.
\{n,\}
At least n occurrences.
\{n\}
Exactly n occurrences.
\(\)
Group parts of regular expressions together.
\n
Matches the preceding value n number of times.

Note how the parentheses and braces metacharacters need a backslash character to escape it in BRE.

Extended Regular Expressions (ERE)

When using extended regular expressions, we must type in the -E option. This group adds more metacharacters to BRE. Furthermore, they don't need a backslash before any brace or parentheses ({n,m}).

{n,m}
Same as BRE, but no \.
(...)
Groupings.
+
match one or more instances of the preceding character or grouping.
?
Match zero or more instances of the preceding character or grouping.
|
OR. Match any instances that come before or after the pipe.

Done skimming through each metacharacter? Great! Let's move on to see some examples and get a more detailed look at how each special character works.

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Ad