Tutorial 9 - Regular Expressions
Content under development
Contents
USING REGULAR EXPRESSIONS
Main Objectives of this Practice Tutorial
- Define the term Regular Expressions
- Explain the difference between Regular Expressions and Filename Expansion
- Explain the purpose of Literal (Simple) Regular Expressions
- Understand and use common symbols for Complex Regular Expressions and their purpose
- Understand and use command symbols for Extended Regular Expressions and their purpose
- List several Linux commands that can use regular expressions
Tutorial Reference Material
Course Notes |
Linux Command/Shortcut Reference | ||
Course Notes:
|
Regular Expressions
|
Linux Commands |
KEY CONCEPTS
Regular Expressions
A regular expression is a combination of two types of characters: literals and special characters.
Strings of text can be compared to this pattern to see if there is a match.
This usually refers to text that is contained inside a file or text as a result
of issuing Linux commands using a Linux pipeline command.
Literal (Simple) Regular Expressions
The simplest regular expression is a series of letters and numbers, (tabs or spaces).
A simple (literal) regular expression consists of normal characters, which used to match patterns.
Although there are many Linux commands that use regular expressions, the grep command is a useful command to learn how to display matches of patterns of strings within text files.
For example:
grep Linux document.txt
Complex / Extended Regular Expressions
Complex Regular Expressions
The problem with just using simple (literal) regular expressions is that only simple or general patterns are matched.
Complex Regular Expressions use symbols to help match text for more precise (complex) patterns.
The most common complex regular expression symbols are displayed below:
- Anchors: ^ , $
Match lines the begin (^) or end ($) with a pattern. - Single Character: .
Represents a single character that can be any type of character. - Character Class: [ ] , [^ ]
Represents a single character but with restrictions. - Zero or More Occurrence: *
Zero or more occurrences of previous character.
- Examples of complex regular expressions are displayed below:
Extended Regular Expressions
Extended Regular Expressions consist of additional special characters to “extend”
the capability of regular expressions. You must use the egrep or grep -E commands
in order to properly use extended regular expressions.
- Repetition: {min,max}
Allows for more precise repetitions. Using braces, you can specify
the minimum and/or maximum number of repetitions.
- Groups: ( )
Allows you to search for repetition for a group of characters, a word, or a phase.
You enclose them within brackets ( ) to specify a group.
- or Condition: |
Can be used with groups to match a variety of character(s), words or phases.
The | symbol is used to separate the variety of character(s) within a group.
- Examples of how to use extended regular expressions with the egrep command are displayed below: