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!

Linux for Beginners

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

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

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

Ad