Difference between revisions of "Tutorial 9 - Regular Expressions"

From CDOT Wiki
Jump to: navigation, search
Line 27: Line 27:
  
 
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Linux Command/Shortcut Reference<br>
 
|colspan="2" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|Linux Command/Shortcut Reference<br>
 
|colspan="1" style="font-size:16px;font-weight:bold;border-bottom: thin solid black;border-spacing:0px;padding-left:15px;"|YouTube Videos<br>
 
  
 
|- valign="top" style="padding-left:15px;"
 
|- valign="top" style="padding-left:15px;"
  
|colspan="2" |Course Notes:<ul><li>[https://ict.senecacollege.ca/~murray.saul/uli101/ULI101-Week9.pdf PDF] | [https://ict.senecacollege.ca/~murray.saul/uli101/ULI101-Week9.pptx PPTX]</li></ul>
+
|colspan="2" |Course Notes:<ul><li>[https://matrix.senecacollege.ca/~osl640/slides/OSL640-Week9.pdf PDF] | [https://matrix.senecacollege.ca/~osl640/slides/OSL640-Week9.pptx PPTX]</li></ul>
  
  
Line 49: Line 47:
 
* [https://linux.die.net/man/1/wget wget]
 
* [https://linux.die.net/man/1/wget wget]
  
|colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=-2pwLHcvCsU&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=12 Using grep Command with Regular Expressions]</li></ul>
 
 
|}
 
|}
  

Revision as of 21:26, 25 October 2021

Content under development

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

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

A simple (literal) regular expression is a series of letters and numbers (tabs or spaces).

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:
Example of using anchors.
Example of matching by character(s).
Example of using character class.
Example of matching zero or more occurrence of preceding character.












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:

Example of using repetition.
Example of using groups.
Example of using or condition with groups.





















INVESTIGATION 1: SIMPLE & COMPLEX REGULAR EXPRESSIONS

INVESTIGATION 2: EXTENDED REGULAR EXPRESSIONS

INVESTIGATION 3: OTHER COMMANDS THAT USE REGULAR EXPRESSIONS

LINUX PRACTICE QUESTIONS