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?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

How Linux Works

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

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources