SlideShare a Scribd company logo
Colloquium - grep
v1.0
A. Magee
April 6, 2010
1 / 16
Colloquium - grep, v1.0
A. Magee
Outline
1 Introduction
What does grep offer?
When should I use grep?
2 Understanding Regular Expressions
Class Basics
Quantifiers & Grouping
Online Tools
Examples
3 Using Regular Expressions With grep
2 / 16
Colloquium - grep, v1.0
A. Magee
Outline
1 Introduction
What does grep offer?
When should I use grep?
2 Understanding Regular Expressions
Class Basics
Quantifiers & Grouping
Online Tools
Examples
3 Using Regular Expressions With grep
2 / 16
Colloquium - grep, v1.0
A. Magee
Outline
1 Introduction
What does grep offer?
When should I use grep?
2 Understanding Regular Expressions
Class Basics
Quantifiers & Grouping
Online Tools
Examples
3 Using Regular Expressions With grep
2 / 16
Colloquium - grep, v1.0
A. Magee
Introduction What?
What does grep offer?
grep matches regular expressions.
Your first question should be“What is a regular expression?”
A regular expression is a language pattern.
grep and REs allow us to find complex things in text.
Complex is relative and can vary from a single character to an IP
address.
Single character complex: [ajk+0-]
IP complex: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
3 / 16
Colloquium - grep, v1.0
A. Magee
Introduction What?
What does grep offer?
grep matches regular expressions.
Your first question should be“What is a regular expression?”
A regular expression is a language pattern.
grep and REs allow us to find complex things in text.
Complex is relative and can vary from a single character to an IP
address.
Single character complex: [ajk+0-]
IP complex: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
3 / 16
Colloquium - grep, v1.0
A. Magee
Introduction What?
What does grep offer?
grep matches regular expressions.
Your first question should be“What is a regular expression?”
A regular expression is a language pattern.
grep and REs allow us to find complex things in text.
Complex is relative and can vary from a single character to an IP
address.
Single character complex: [ajk+0-]
IP complex: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
3 / 16
Colloquium - grep, v1.0
A. Magee
Introduction When?
When should I use grep?
Always!
Unless you find some better tool.
P.S. - grep stands for g/re/p, an ed command that means global/reg
ex/print
4 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Class Basics
Class Basics
A character class is a symbol or collection of symbols that describes a
group of characters.
. (period): This matches any single character.
[...]: This matches any one character in the set.
[aeiou] matches one of the vowels.
[a-z] matches one of the lowercase alphabet.
[0-5] matches one numeral 0 through 5.
You will not remember all of these until you use them often, but
there are many special classes that can save you some typing.
5 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Class Basics
Common Classes
Special Class Meaning Simple RE
d Digit characters [0-9]
D Non-digit characters [ˆ0-9]
w Word characters [a-zA-Z 0-9]
W Non-word characters [ˆa-zA-Z 0-9]
s Whitespace characters characters [fnrt]
S Non-space characters [ˆfnrt]
b Word boundary
The word boundary class is very special as it is zero length and matches
transitions between s and w and vice versa.
6 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Class Basics
More Common Classes
Special Class Meaning Simple RE
[:alpha:] All alphabetic characters [a-zA-Z]
[:alnum:] All alphabetic and numeric [a-zA-Z0-9]
[:blank:] Tab and space
[:cntrl:] Control characters [x00-x1Fx7F]
[:digit:] A numeric digit [0-9]
[:graph:] Any visible character [x21-x7E]
[:lower:] Lowercase characters [a-z]
[:print:] Printables (i.e. no controls) [x20-x7E]
[:punct:] Punctuation & symbols [!”#$%&’()*+,-./:;<=>?
@[ ]ˆ ‘{|}∼]
[:space:] Space, tab, newline, etc [ trnvf]
[:upper:] Uppercase characters [A-Z]
[:word:] Word characters [a-zA-Z0-9 ]
[:xdigit:] Hex digits [A-Fa-f0-9]
7 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Quantifiers & Grouping
Quantifiers & Grouping
Quantifiers are how a RE counts things.
? Exactly zero or one occurrence
* Zero or more occurrences
+ One or more occurrences
*? Zero or more occurrences non-greedy
+? One or more occurrences non-greedy
{x} Exactly x occurrences
{x,} At least x occurrences
{x,y} At least x but no more than y occurrences
Grouping is used to collect patterns together and to create
back-references. A group is simply a set of parentheses ().
8 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Online Tools
Helpful Tools
The best way to understand the rest of this presentation is to see what is
being matched live. Here are some online tools that work for our needs.
RegExr - www.gskinner.com/RegExr
beware Flash, but it works well
regexpal - regexpal.com
very simple
reanimator - osteele.com/tools/reanimator
beware Flash, recommend CS 4/570 first
rubular - rubular.com
nice on-page reference
9 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE
Let’s skip trivial REs and get on to something useful. These may be more
complex than you’re used to but the quicker you are able to read long,
complex REs the better. This is a nice, but not perfect, email address
matcher.
[[:alnum:]][[:word:].%+-]*@(?:[[:alnum:]-]+.)+[[:alpha:]]{2,4}
[[:alnum:]][[:word:].%+-]*
Match a word that doesn’t start with [.%+-].
@(?:[[:alnum:]-]+.)+
Match the @ symbol and any number of subdomains followed by
periods.
[[:alpha:]]{2,4}
Match the top level domain of 2, 3 or 4 characters.
10 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE - Part 2
Let’s examine the first part.
[[:alnum:]][[:word:].%+-]*
[[:alnum:]] - Must start with an alphanumeric character.
NB: All [: ... :] classes must live in a set like [[: ... :]].
[[:word:].%+-] - Other characters maybe a ‘word’ character,
a literal space, percent symbol, plus symbol or a dash.
NB: The period must be escaped because it has special meaning.
* - repeat the previous set zero or more times.
11 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE - Part 2
Let’s examine the first part.
[[:alnum:]][[:word:].%+-]*
[[:alnum:]] - Must start with an alphanumeric character.
NB: All [: ... :] classes must live in a set like [[: ... :]].
[[:word:].%+-] - Other characters maybe a ‘word’ character,
a literal space, percent symbol, plus symbol or a dash.
NB: The period must be escaped because it has special meaning.
* - repeat the previous set zero or more times.
11 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE - Part 2
Let’s examine the first part.
[[:alnum:]][[:word:].%+-]*
[[:alnum:]] - Must start with an alphanumeric character.
NB: All [: ... :] classes must live in a set like [[: ... :]].
[[:word:].%+-] - Other characters maybe a ‘word’ character,
a literal space, percent symbol, plus symbol or a dash.
NB: The period must be escaped because it has special meaning.
* - repeat the previous set zero or more times.
11 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE - Part 3
Now the second part, the subdomains, sub-subdomains, etc.
@(?:[[:alnum:]-]+.)+
@ - Well that literally matches the ‘at’ character.
The parenthesis denote the beginning of a group.
The ?: is a confusing notation that suppresses the creation of a
back reference. It is here so you’ll know of it, but it is rarely needed.
Again we see a special class for alphanumerics, but we’ve also
included a dash. The plus symbol tells us to look for one or more of
these characters, followed by a period.
And lastly we close the group and the plus symbol now tells us to
look for one or more of these groups.
12 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE - Part 3
Now the second part, the subdomains, sub-subdomains, etc.
@(?:[[:alnum:]-]+.)+
@ - Well that literally matches the ‘at’ character.
The parenthesis denote the beginning of a group.
The ?: is a confusing notation that suppresses the creation of a
back reference. It is here so you’ll know of it, but it is rarely needed.
Again we see a special class for alphanumerics, but we’ve also
included a dash. The plus symbol tells us to look for one or more of
these characters, followed by a period.
And lastly we close the group and the plus symbol now tells us to
look for one or more of these groups.
12 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE - Part 3
Now the second part, the subdomains, sub-subdomains, etc.
@(?:[[:alnum:]-]+.)+
@ - Well that literally matches the ‘at’ character.
The parenthesis denote the beginning of a group.
The ?: is a confusing notation that suppresses the creation of a
back reference. It is here so you’ll know of it, but it is rarely needed.
Again we see a special class for alphanumerics, but we’ve also
included a dash. The plus symbol tells us to look for one or more of
these characters, followed by a period.
And lastly we close the group and the plus symbol now tells us to
look for one or more of these groups.
12 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE - Part 3
Now the second part, the subdomains, sub-subdomains, etc.
@(?:[[:alnum:]-]+.)+
@ - Well that literally matches the ‘at’ character.
The parenthesis denote the beginning of a group.
The ?: is a confusing notation that suppresses the creation of a
back reference. It is here so you’ll know of it, but it is rarely needed.
Again we see a special class for alphanumerics, but we’ve also
included a dash. The plus symbol tells us to look for one or more of
these characters, followed by a period.
And lastly we close the group and the plus symbol now tells us to
look for one or more of these groups.
12 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your First RE - Part 4
Finally the third part, the domain.
[[:alpha:]]{2,4}
We’ll now this part is easy. Just match 2, 3 or 4 alphabetical
characters.
13 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your Second RE
Now we’ll look at a RE that can help use build a header file for a c
program file, given that some neglectful programmer has failed to design
his/her c program properly. This will be a quicker example.
ˆ[ws]*([ws*&,]*)s*{
ˆ[ws]*(
At the beginning of a line match some keywords and types and
the function name and then literal parenthesis.
[ws*&,]*
Match some more words, keywords, variable modifiers and commas.
)s*{
Finally match the closing parenthesis, some whitespace and the
left curly brace, denoting the start of the function body.
14 / 16
Colloquium - grep, v1.0
A. Magee
Regular Expressions Examples
Your Second RE - Fine Details
ˆ[ws]*([ws*&,]*)s*{
In general, most RE parsers will not match across multiple lines, even
though the s class matches the newline character. This is very
bothersome but is easily overcome by using pcregrep. pcre is Perl
Compatible Regular Expression. This is all I will ever say about Perl.
Notice that the literal * must be escaped like so, *.
As must the parentheses due to their special RE meaning.
Escaping so many characters is very annoying, but unfortunately it is
necessary.
15 / 16
Colloquium - grep, v1.0
A. Magee
Appendix
4 Appendix
16 / 16
Colloquium - grep, v1.0
A. Magee

More Related Content

What's hot

Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
Chirag Shetty
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
shailaja30
 
Grep
GrepGrep
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)
Ali ٍSattar
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
Nirajan Pant
 
Python
PythonPython
Python
Kumar Gaurav
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
nitamhaske
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
John(Qiang) Zhang
 
Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 
Python language data types
Python language data typesPython language data types
Python language data types
Hoang Nguyen
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular Expressions
OCSI
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore Haskell
Bryan O'Sullivan
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
Ranel Padon
 
Processing Regex Python
Processing Regex PythonProcessing Regex Python
Processing Regex Python
primeteacher32
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
Satya Narayana
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
Logan Palanisamy
 
String & its application
String & its applicationString & its application
String & its application
Tech_MX
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
Max Kleiner
 

What's hot (20)

Python (regular expression)
Python (regular expression)Python (regular expression)
Python (regular expression)
 
Introduction To Python
Introduction To  PythonIntroduction To  Python
Introduction To Python
 
Grep
GrepGrep
Grep
 
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
 
Python
PythonPython
Python
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
Python strings
Python stringsPython strings
Python strings
 
Python language data types
Python language data typesPython language data types
Python language data types
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular Expressions
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore Haskell
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Python Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular ExpressionsPython Programming - XI. String Manipulation and Regular Expressions
Python Programming - XI. String Manipulation and Regular Expressions
 
Processing Regex Python
Processing Regex PythonProcessing Regex Python
Processing Regex Python
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Awk essentials
Awk essentialsAwk essentials
Awk essentials
 
String & its application
String & its applicationString & its application
String & its application
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 

Viewers also liked

Cut command in unix
Cut command in unixCut command in unix
Cut command in unix
Manoj Pradhan
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
Nihar Ranjan Paital
 
Learning Grep
Learning GrepLearning Grep
Learning Grep
Vikas Kumar CSM®
 
Unix command quickref
Unix command quickrefUnix command quickref
Unix command quickref
Arduino Aficionado
 
4, grep
4, grep4, grep
4, grep
ted-xu
 
Take Your Life To The Next Level
Take Your Life To The Next LevelTake Your Life To The Next Level
Take Your Life To The Next Level
tricycle
 

Viewers also liked (6)

Cut command in unix
Cut command in unixCut command in unix
Cut command in unix
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
 
Learning Grep
Learning GrepLearning Grep
Learning Grep
 
Unix command quickref
Unix command quickrefUnix command quickref
Unix command quickref
 
4, grep
4, grep4, grep
4, grep
 
Take Your Life To The Next Level
Take Your Life To The Next LevelTake Your Life To The Next Level
Take Your Life To The Next Level
 

Similar to Grep Introduction

Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raghu nath
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
Sopan Shewale
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
ajoy21
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
Jun Shimizu
 
Questions4
Questions4Questions4
Questions4
hccit
 
Unix
UnixUnix
Unix
lilututu
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
James Armes
 
Template Haskell
Template HaskellTemplate Haskell
Template Haskell
Sergey Stretovich
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015
Sandy Smith
 
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017
Sandy Smith
 
RegEx Book.pdf
RegEx Book.pdfRegEx Book.pdf
RegEx Book.pdf
OmkarHankare4
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
Antony Stubbs
 
Regex startup
Regex startupRegex startup
Regex startup
PayPal
 
Brogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitBrogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and Git
Ron Reiter
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
Sandy Smith
 
MMBJ Shanzhai Culture
MMBJ Shanzhai CultureMMBJ Shanzhai Culture
MMBJ Shanzhai Culture
MobileMonday Beijing
 
Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5
Oregon Law Practice Management
 
Agapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.comAgapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.com
Antonio Silva
 
Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1
Alejandra Perez
 

Similar to Grep Introduction (20)

Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Perl Presentation
Perl PresentationPerl Presentation
Perl Presentation
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
Questions4
Questions4Questions4
Questions4
 
Unix
UnixUnix
Unix
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
Template Haskell
Template HaskellTemplate Haskell
Template Haskell
 
Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015Don't Fear the Regex - Northeast PHP 2015
Don't Fear the Regex - Northeast PHP 2015
 
Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017Don't Fear the Regex WordCamp DC 2017
Don't Fear the Regex WordCamp DC 2017
 
RegEx Book.pdf
RegEx Book.pdfRegEx Book.pdf
RegEx Book.pdf
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 
Regex startup
Regex startupRegex startup
Regex startup
 
Brogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and GitBrogramming - Python, Bash for Data Processing, and Git
Brogramming - Python, Bash for Data Processing, and Git
 
Don't Fear the Regex LSP15
Don't Fear the Regex LSP15Don't Fear the Regex LSP15
Don't Fear the Regex LSP15
 
MMBJ Shanzhai Culture
MMBJ Shanzhai CultureMMBJ Shanzhai Culture
MMBJ Shanzhai Culture
 
Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5Washington Practitioners Significant Changes To Rpc 1.5
Washington Practitioners Significant Changes To Rpc 1.5
 
Agapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.comAgapornis Mansos - www.criadourosudica.blogspot.com
Agapornis Mansos - www.criadourosudica.blogspot.com
 
Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1Paulo Freire Pedagpogia 1
Paulo Freire Pedagpogia 1
 

Recently uploaded

GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
lorraineandreiamcidl
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Undress Baby
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 

Recently uploaded (20)

GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptxLORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
LORRAINE ANDREI_LEQUIGAN_HOW TO USE WHATSAPP.pptx
 
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit ParisNeo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
Neo4j - Product Vision and Knowledge Graphs - GraphSummit Paris
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdfRevolutionizing Visual Effects Mastering AI Face Swaps.pdf
Revolutionizing Visual Effects Mastering AI Face Swaps.pdf
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 

Grep Introduction

  • 1. Colloquium - grep v1.0 A. Magee April 6, 2010 1 / 16 Colloquium - grep, v1.0 A. Magee
  • 2. Outline 1 Introduction What does grep offer? When should I use grep? 2 Understanding Regular Expressions Class Basics Quantifiers & Grouping Online Tools Examples 3 Using Regular Expressions With grep 2 / 16 Colloquium - grep, v1.0 A. Magee
  • 3. Outline 1 Introduction What does grep offer? When should I use grep? 2 Understanding Regular Expressions Class Basics Quantifiers & Grouping Online Tools Examples 3 Using Regular Expressions With grep 2 / 16 Colloquium - grep, v1.0 A. Magee
  • 4. Outline 1 Introduction What does grep offer? When should I use grep? 2 Understanding Regular Expressions Class Basics Quantifiers & Grouping Online Tools Examples 3 Using Regular Expressions With grep 2 / 16 Colloquium - grep, v1.0 A. Magee
  • 5. Introduction What? What does grep offer? grep matches regular expressions. Your first question should be“What is a regular expression?” A regular expression is a language pattern. grep and REs allow us to find complex things in text. Complex is relative and can vary from a single character to an IP address. Single character complex: [ajk+0-] IP complex: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3} (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) 3 / 16 Colloquium - grep, v1.0 A. Magee
  • 6. Introduction What? What does grep offer? grep matches regular expressions. Your first question should be“What is a regular expression?” A regular expression is a language pattern. grep and REs allow us to find complex things in text. Complex is relative and can vary from a single character to an IP address. Single character complex: [ajk+0-] IP complex: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3} (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) 3 / 16 Colloquium - grep, v1.0 A. Magee
  • 7. Introduction What? What does grep offer? grep matches regular expressions. Your first question should be“What is a regular expression?” A regular expression is a language pattern. grep and REs allow us to find complex things in text. Complex is relative and can vary from a single character to an IP address. Single character complex: [ajk+0-] IP complex: (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3} (25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) 3 / 16 Colloquium - grep, v1.0 A. Magee
  • 8. Introduction When? When should I use grep? Always! Unless you find some better tool. P.S. - grep stands for g/re/p, an ed command that means global/reg ex/print 4 / 16 Colloquium - grep, v1.0 A. Magee
  • 9. Regular Expressions Class Basics Class Basics A character class is a symbol or collection of symbols that describes a group of characters. . (period): This matches any single character. [...]: This matches any one character in the set. [aeiou] matches one of the vowels. [a-z] matches one of the lowercase alphabet. [0-5] matches one numeral 0 through 5. You will not remember all of these until you use them often, but there are many special classes that can save you some typing. 5 / 16 Colloquium - grep, v1.0 A. Magee
  • 10. Regular Expressions Class Basics Common Classes Special Class Meaning Simple RE d Digit characters [0-9] D Non-digit characters [ˆ0-9] w Word characters [a-zA-Z 0-9] W Non-word characters [ˆa-zA-Z 0-9] s Whitespace characters characters [fnrt] S Non-space characters [ˆfnrt] b Word boundary The word boundary class is very special as it is zero length and matches transitions between s and w and vice versa. 6 / 16 Colloquium - grep, v1.0 A. Magee
  • 11. Regular Expressions Class Basics More Common Classes Special Class Meaning Simple RE [:alpha:] All alphabetic characters [a-zA-Z] [:alnum:] All alphabetic and numeric [a-zA-Z0-9] [:blank:] Tab and space [:cntrl:] Control characters [x00-x1Fx7F] [:digit:] A numeric digit [0-9] [:graph:] Any visible character [x21-x7E] [:lower:] Lowercase characters [a-z] [:print:] Printables (i.e. no controls) [x20-x7E] [:punct:] Punctuation & symbols [!”#$%&’()*+,-./:;<=>? @[ ]ˆ ‘{|}∼] [:space:] Space, tab, newline, etc [ trnvf] [:upper:] Uppercase characters [A-Z] [:word:] Word characters [a-zA-Z0-9 ] [:xdigit:] Hex digits [A-Fa-f0-9] 7 / 16 Colloquium - grep, v1.0 A. Magee
  • 12. Regular Expressions Quantifiers & Grouping Quantifiers & Grouping Quantifiers are how a RE counts things. ? Exactly zero or one occurrence * Zero or more occurrences + One or more occurrences *? Zero or more occurrences non-greedy +? One or more occurrences non-greedy {x} Exactly x occurrences {x,} At least x occurrences {x,y} At least x but no more than y occurrences Grouping is used to collect patterns together and to create back-references. A group is simply a set of parentheses (). 8 / 16 Colloquium - grep, v1.0 A. Magee
  • 13. Regular Expressions Online Tools Helpful Tools The best way to understand the rest of this presentation is to see what is being matched live. Here are some online tools that work for our needs. RegExr - www.gskinner.com/RegExr beware Flash, but it works well regexpal - regexpal.com very simple reanimator - osteele.com/tools/reanimator beware Flash, recommend CS 4/570 first rubular - rubular.com nice on-page reference 9 / 16 Colloquium - grep, v1.0 A. Magee
  • 14. Regular Expressions Examples Your First RE Let’s skip trivial REs and get on to something useful. These may be more complex than you’re used to but the quicker you are able to read long, complex REs the better. This is a nice, but not perfect, email address matcher. [[:alnum:]][[:word:].%+-]*@(?:[[:alnum:]-]+.)+[[:alpha:]]{2,4} [[:alnum:]][[:word:].%+-]* Match a word that doesn’t start with [.%+-]. @(?:[[:alnum:]-]+.)+ Match the @ symbol and any number of subdomains followed by periods. [[:alpha:]]{2,4} Match the top level domain of 2, 3 or 4 characters. 10 / 16 Colloquium - grep, v1.0 A. Magee
  • 15. Regular Expressions Examples Your First RE - Part 2 Let’s examine the first part. [[:alnum:]][[:word:].%+-]* [[:alnum:]] - Must start with an alphanumeric character. NB: All [: ... :] classes must live in a set like [[: ... :]]. [[:word:].%+-] - Other characters maybe a ‘word’ character, a literal space, percent symbol, plus symbol or a dash. NB: The period must be escaped because it has special meaning. * - repeat the previous set zero or more times. 11 / 16 Colloquium - grep, v1.0 A. Magee
  • 16. Regular Expressions Examples Your First RE - Part 2 Let’s examine the first part. [[:alnum:]][[:word:].%+-]* [[:alnum:]] - Must start with an alphanumeric character. NB: All [: ... :] classes must live in a set like [[: ... :]]. [[:word:].%+-] - Other characters maybe a ‘word’ character, a literal space, percent symbol, plus symbol or a dash. NB: The period must be escaped because it has special meaning. * - repeat the previous set zero or more times. 11 / 16 Colloquium - grep, v1.0 A. Magee
  • 17. Regular Expressions Examples Your First RE - Part 2 Let’s examine the first part. [[:alnum:]][[:word:].%+-]* [[:alnum:]] - Must start with an alphanumeric character. NB: All [: ... :] classes must live in a set like [[: ... :]]. [[:word:].%+-] - Other characters maybe a ‘word’ character, a literal space, percent symbol, plus symbol or a dash. NB: The period must be escaped because it has special meaning. * - repeat the previous set zero or more times. 11 / 16 Colloquium - grep, v1.0 A. Magee
  • 18. Regular Expressions Examples Your First RE - Part 3 Now the second part, the subdomains, sub-subdomains, etc. @(?:[[:alnum:]-]+.)+ @ - Well that literally matches the ‘at’ character. The parenthesis denote the beginning of a group. The ?: is a confusing notation that suppresses the creation of a back reference. It is here so you’ll know of it, but it is rarely needed. Again we see a special class for alphanumerics, but we’ve also included a dash. The plus symbol tells us to look for one or more of these characters, followed by a period. And lastly we close the group and the plus symbol now tells us to look for one or more of these groups. 12 / 16 Colloquium - grep, v1.0 A. Magee
  • 19. Regular Expressions Examples Your First RE - Part 3 Now the second part, the subdomains, sub-subdomains, etc. @(?:[[:alnum:]-]+.)+ @ - Well that literally matches the ‘at’ character. The parenthesis denote the beginning of a group. The ?: is a confusing notation that suppresses the creation of a back reference. It is here so you’ll know of it, but it is rarely needed. Again we see a special class for alphanumerics, but we’ve also included a dash. The plus symbol tells us to look for one or more of these characters, followed by a period. And lastly we close the group and the plus symbol now tells us to look for one or more of these groups. 12 / 16 Colloquium - grep, v1.0 A. Magee
  • 20. Regular Expressions Examples Your First RE - Part 3 Now the second part, the subdomains, sub-subdomains, etc. @(?:[[:alnum:]-]+.)+ @ - Well that literally matches the ‘at’ character. The parenthesis denote the beginning of a group. The ?: is a confusing notation that suppresses the creation of a back reference. It is here so you’ll know of it, but it is rarely needed. Again we see a special class for alphanumerics, but we’ve also included a dash. The plus symbol tells us to look for one or more of these characters, followed by a period. And lastly we close the group and the plus symbol now tells us to look for one or more of these groups. 12 / 16 Colloquium - grep, v1.0 A. Magee
  • 21. Regular Expressions Examples Your First RE - Part 3 Now the second part, the subdomains, sub-subdomains, etc. @(?:[[:alnum:]-]+.)+ @ - Well that literally matches the ‘at’ character. The parenthesis denote the beginning of a group. The ?: is a confusing notation that suppresses the creation of a back reference. It is here so you’ll know of it, but it is rarely needed. Again we see a special class for alphanumerics, but we’ve also included a dash. The plus symbol tells us to look for one or more of these characters, followed by a period. And lastly we close the group and the plus symbol now tells us to look for one or more of these groups. 12 / 16 Colloquium - grep, v1.0 A. Magee
  • 22. Regular Expressions Examples Your First RE - Part 4 Finally the third part, the domain. [[:alpha:]]{2,4} We’ll now this part is easy. Just match 2, 3 or 4 alphabetical characters. 13 / 16 Colloquium - grep, v1.0 A. Magee
  • 23. Regular Expressions Examples Your Second RE Now we’ll look at a RE that can help use build a header file for a c program file, given that some neglectful programmer has failed to design his/her c program properly. This will be a quicker example. ˆ[ws]*([ws*&,]*)s*{ ˆ[ws]*( At the beginning of a line match some keywords and types and the function name and then literal parenthesis. [ws*&,]* Match some more words, keywords, variable modifiers and commas. )s*{ Finally match the closing parenthesis, some whitespace and the left curly brace, denoting the start of the function body. 14 / 16 Colloquium - grep, v1.0 A. Magee
  • 24. Regular Expressions Examples Your Second RE - Fine Details ˆ[ws]*([ws*&,]*)s*{ In general, most RE parsers will not match across multiple lines, even though the s class matches the newline character. This is very bothersome but is easily overcome by using pcregrep. pcre is Perl Compatible Regular Expression. This is all I will ever say about Perl. Notice that the literal * must be escaped like so, *. As must the parentheses due to their special RE meaning. Escaping so many characters is very annoying, but unfortunately it is necessary. 15 / 16 Colloquium - grep, v1.0 A. Magee
  • 25. Appendix 4 Appendix 16 / 16 Colloquium - grep, v1.0 A. Magee