Lab 8: Regular Expressions

Slides:



Advertisements
Similar presentations
Specifying Languages Our aim is to be able to specify languages for use in the computer. The sketch of the FSA is easy for us to understand, but difficult.
Advertisements

CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 31: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Computational Language Finite State Machines and Regular Expressions.
Grep, comm, and uniq. The grep Command The grep command allows a user to search for specific text inside a file. The grep command will find all occurrences.
Filters using Regular Expressions grep: Searching a Pattern.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
Advanced File Processing
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
System Programming Regular Expressions Regular Expressions
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
Globalisation & Computer systems Week 7 Text processes and globalisation part 1: Sorting strings: collation Searching strings and regular expressions Practical:
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
Module 6 – Redirections, Pipes and Power Tools.. STDin 0 STDout 1 STDerr 2 Redirections.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
January 23, 2007Spring Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Regular Expression - Intro Patterns that define a set of strings (or, pieces of a string) Not wildcards (similar notion, but different thing) Used by utilities.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Sys Prog & Scrip - Heriot Watt Univ 1 Systems Programming & Scripting Lecture 12: Introduction to Scripting & Regular Expressions.
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
By Corey Stokes 9/14/10. What is grep? Global Regular Expression Print grep is a command line search utility in Unix Try: Search for a word in a.cpp file.
Specifying Languages Our aim is to be able to specify languages for use in the computer. The sketch of an FSA is easy for us to understand, but difficult.
16-Dec-15Advanced Programming Spring 2002 sed and awk Henning Schulzrinne Dept. of Computer Science Columbia University.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
Natural Language Processing Lecture 4 : Regular Expressions and Automata.
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
FILTERS USING REGULAR EXPRESSIONS – grep and sed.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
CSC 352– Unix Programming, Fall 2011 November 8, 2011, Week 11, a useful subset of regular expressions, grep and sed, parts of Chapter 11.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Awk 2 – more awk. AWK INVOCATION AND OPERATION the "-F" option allows changing Awk's "field separator" character. Awk regards each line of input data.
Lecture Three: Finite Automata Finite Automata, Lecture 3, slide 1 Amjad Ali.
CIRC Summer School 2016 Baowei Liu
Regular Expressions Copyright Doug Maxwell (
Regular expressions, egrep, and sed
CSC 352– Unix Programming, Spring 2016
Regular Expressions ICCM 2017
LINUX LANGUAGE MULTIPLE CHOICE QUESTION SET-5
Regular expressions, egrep, and sed
Looking for Patterns - Finding them with Regular Expressions
CIRC Summer School 2017 Baowei Liu
CIRC Winter Boot Camp 2017 Baowei Liu
Regular Expression - Intro
CSE 390a Lecture 7 Regular expressions, egrep, and sed
CSC 352– Unix Programming, Fall 2012
The ‘grep’ Command Colin Masterson.
Guide To UNIX Using Linux Third Edition
Specifying Languages Our aim is to be able to specify languages for use in the computer. The sketch of the FSA is easy for us to understand, but difficult.
CSE 390a Lecture 7 Regular expressions, egrep, and sed
An Overview of Grep and Regular Expression
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Regular expressions, egrep, and sed
Regular expressions, egrep, and sed
CSE 303 Concepts and Tools for Software Development
Regular Expressions and Grep
Lab 7: Filtering.
1.5 Regular Expressions (REs)
CSE 390a Lecture 7 Regular expressions, egrep, and sed
Regular Expressions.
Presentation transcript:

Lab 8: Regular Expressions Enter the world of Black Magic

What is a regular expression? A regular expression is a sequence of symbols and characters expressing a string or pattern to be searched for within a longer piece of text Or in theory of computation terms: A regular expression is an algebraic formula whose value is a pattern consisting of a set of strings, called the language of the expression [This is the confusion definition that we don’t need to learn]

Examples Files on GitHub We will be using the same files from Lab 7 to learn more about regex in this lab

Grep Grep is the most common regex command used on the command line Grep stands for global regular expression print The syntax is grep [options] pattern [file] In essence, you define a search pattern and a file and then the shell will read through the file to find instances of that pattern

Common Regex Syntax The following are the various characters that are used in a regex: . (dot) – a single character ? - preceding character matches zero or one times * - preceding character matches zero or more times + - preceding character matches one or more times [a-z] – character is included in the range of characters specified by the brackets [^ a-z] – character is not one of those included in the brackets in that range {n} – preceding character matches exactly n times {n, m} – preceding character matches at least n times, but not more than m times ^ – matches at the beginning of the line $ – matches at the end of the line

Grep Examples grep ‘firefox’ ps-output.txt grep ‘libreoffice’ ps-output.txt grep ‘f.x’ period.txt grep ‘…..’ period.txt Grep -x ‘[a-z]\+’ random-passwords.txt grep -x ‘[0-9]\+’ random-passwords.txt grep ‘[a-c]\+’ random-passwords.txt Grep ‘[T]$’ random-alphanumeric.txt Grep ‘^y.*[0-9]’ random-alphanumeric.txt

Other Commands There are other numerous commands that you can use other than grep to filter through files Comm Awk Sed [this is very complicated]

Real World Example In Spring 2018, I was tasked with making an anti- virus which could detect malicious files I used various filtering commands and several regex commands in order to filter out unnecessary inputs and keep the desired input. Code: