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.
Matches at least n occurrences, but at most m.
At least n occurrences.
Exactly n occurrences.
Group parts of regular expressions together.
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}).

Same as BRE, but no \.
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.

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

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