SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
Regular Expressions
                      What are they and what can they do for you?




Thursday, February 7, 13
b[A-Z0-9._%+-]+@[A-
                     Z0-9.-]+.[A-Z]{2-4}b



                            Source: http:/ /www.regular-
                             expressions.info/email.html
Thursday, February 7, 13
So...what is a
                           regular expression?




Thursday, February 7, 13
A Regular Expression is
                     a pattern for a string.




Thursday, February 7, 13
What can you do with
                           Regular Expressions?
                              Test a string
                             Extract a string
                             Change a string


Thursday, February 7, 13
Creating Regular
                              Expressions

                           /RegularExpression/




Thursday, February 7, 13
Scenario #1
                           Testing Strings




Thursday, February 7, 13
Examples from
  “Programming Ruby” (PickAxe)



Thursday, February 7, 13
“dog and cat”




Thursday, February 7, 13
“dog and cat”

                               /cat/




Thursday, February 7, 13
“dog and cat”

                               /cat/

                       /cat/ =~ “dog and cat”


Thursday, February 7, 13
=~
                           /cat/ =~ “dog and cat”


Thursday, February 7, 13
Thursday, February 7, 13
Thursday, February 7, 13
match
             /cat/.match(“dog and cat”)


Thursday, February 7, 13
Valid Emails




Thursday, February 7, 13
thing@thing.thing




Thursday, February 7, 13
Rubular




Thursday, February 7, 13
Alternation
                           /Nell | Brandon/




Thursday, February 7, 13
.
                           Metacharacters

                           any single character
                           character can appear
              *            any number of times

Thursday, February 7, 13
/.*/
                           matches ANYTHING



Thursday, February 7, 13
Range
                           [a-d] [1-4]



Thursday, February 7, 13
Shorthand!

                 w stands for any word
                 character.

                 It’s the same as:
                 [a-zA-Z0-9_]
Thursday, February 7, 13
Metacharacters

              +             character occurs
                           one or more times




Thursday, February 7, 13
Thursday, February 7, 13
Scenario #2
                           Extracting Strings




Thursday, February 7, 13
Shorthand!

                  d stands for any digit

                  D stands for any non-
                  digit

Thursday, February 7, 13
Repetition!

                         d{3}
               Looks for exactly 3 digits




Thursday, February 7, 13
Make It Optional!

                                      ?
                             Makes a character
                           optional. It can occur
                                0 or 1 time.

Thursday, February 7, 13
Scenario #3
                           Changing Strings




Thursday, February 7, 13
“Brandon is the teacher
             of the class right now.
             Brandon is teaching about
             regular expressions.”


Thursday, February 7, 13
sub(/pattern/, “text”)




Thursday, February 7, 13
gsub(/pattern/, “text”)

              (replace ALL the matches)

Thursday, February 7, 13
sub and gsub create new
             strings.
             sub! and gsub! change
             the original strings


Thursday, February 7, 13
Welcome to the
                           wonderful world of
                           Regular Expressions!



Thursday, February 7, 13

Contenu connexe

Tendances

Tendances (14)

رحمه حميد الصبحي
رحمه حميد الصبحيرحمه حميد الصبحي
رحمه حميد الصبحي
 
Week 2-syntax student copy
Week 2-syntax student copyWeek 2-syntax student copy
Week 2-syntax student copy
 
Ms nadia zia & Ayesha Khan
Ms nadia zia & Ayesha KhanMs nadia zia & Ayesha Khan
Ms nadia zia & Ayesha Khan
 
Ms nadia zia (3) copy
Ms nadia zia (3)   copyMs nadia zia (3)   copy
Ms nadia zia (3) copy
 
Week 2.2 syntax cont.
Week 2.2 syntax cont.Week 2.2 syntax cont.
Week 2.2 syntax cont.
 
Possessive
PossessivePossessive
Possessive
 
Np pre modification
Np pre modificationNp pre modification
Np pre modification
 
syntax
syntaxsyntax
syntax
 
E10 oct20 2010
E10 oct20  2010E10 oct20  2010
E10 oct20 2010
 
Week 2 session 2
Week 2 session 2Week 2 session 2
Week 2 session 2
 
"Say It" Test - D ... Alphabet Letters
"Say It" Test - D  ... Alphabet Letters"Say It" Test - D  ... Alphabet Letters
"Say It" Test - D ... Alphabet Letters
 
Tree diagrams
Tree diagramsTree diagrams
Tree diagrams
 
Adverbials and other related matters work 2015
Adverbials and other related matters work 2015Adverbials and other related matters work 2015
Adverbials and other related matters work 2015
 
Pronouns and the verb to be
Pronouns and the verb to bePronouns and the verb to be
Pronouns and the verb to be
 

En vedette

En vedette (15)

Beneath the Surface - Rubyconf 2013
Beneath the Surface - Rubyconf 2013Beneath the Surface - Rubyconf 2013
Beneath the Surface - Rubyconf 2013
 
The rsua
The rsuaThe rsua
The rsua
 
First Do No Harm: Surgical Refactoring (extended edition)
First Do No Harm: Surgical Refactoring (extended edition)First Do No Harm: Surgical Refactoring (extended edition)
First Do No Harm: Surgical Refactoring (extended edition)
 
Moleskine places
Moleskine placesMoleskine places
Moleskine places
 
Refactoring Infrastructure Code
Refactoring Infrastructure CodeRefactoring Infrastructure Code
Refactoring Infrastructure Code
 
Public Supermarket: The Insider's Tour
Public Supermarket: The Insider's TourPublic Supermarket: The Insider's Tour
Public Supermarket: The Insider's Tour
 
Diseño de aplic web (Marvin y Evelyn)
Diseño de aplic web (Marvin y Evelyn)Diseño de aplic web (Marvin y Evelyn)
Diseño de aplic web (Marvin y Evelyn)
 
LONDON,OLD AND NEW
LONDON,OLD AND NEWLONDON,OLD AND NEW
LONDON,OLD AND NEW
 
First Do No Harm: Surgical Refactoring
First Do No Harm: Surgical RefactoringFirst Do No Harm: Surgical Refactoring
First Do No Harm: Surgical Refactoring
 
Devops: A History
Devops: A HistoryDevops: A History
Devops: A History
 
Beneath the Surface: Regular Expressions in Ruby
Beneath the Surface: Regular Expressions in RubyBeneath the Surface: Regular Expressions in Ruby
Beneath the Surface: Regular Expressions in Ruby
 
Beyond the Basics: Regular Expressions in Ruby
Beyond the Basics: Regular Expressions in RubyBeyond the Basics: Regular Expressions in Ruby
Beyond the Basics: Regular Expressions in Ruby
 
A Supermarket of Your Own: Running a Private Chef Supermarket
A Supermarket of Your Own: Running a Private Chef SupermarketA Supermarket of Your Own: Running a Private Chef Supermarket
A Supermarket of Your Own: Running a Private Chef Supermarket
 
Test Driven Development: A Love Story
Test Driven Development: A Love StoryTest Driven Development: A Love Story
Test Driven Development: A Love Story
 
Refactoring terraform
Refactoring terraformRefactoring terraform
Refactoring terraform
 

Plus de Nell Shamrell-Harrington (14)

This Week in Rust: 400 Issues and Counting!
This Week in Rust: 400 Issues and Counting!This Week in Rust: 400 Issues and Counting!
This Week in Rust: 400 Issues and Counting!
 
The Rust Borrow Checker
The Rust Borrow CheckerThe Rust Borrow Checker
The Rust Borrow Checker
 
Higher. Faster. Stronger. Your Applications with Habitat
Higher. Faster. Stronger. Your Applications with HabitatHigher. Faster. Stronger. Your Applications with Habitat
Higher. Faster. Stronger. Your Applications with Habitat
 
Habitat Service Discovery
Habitat Service DiscoveryHabitat Service Discovery
Habitat Service Discovery
 
Web Operations101
Web Operations101Web Operations101
Web Operations101
 
Rust Traits And You: A Deep Dive
Rust Traits And You: A Deep DiveRust Traits And You: A Deep Dive
Rust Traits And You: A Deep Dive
 
Rust, Redis, and Protobuf - Oh My!
Rust, Redis, and Protobuf - Oh My!Rust, Redis, and Protobuf - Oh My!
Rust, Redis, and Protobuf - Oh My!
 
Containers, Virtual Machines, and Bare Metal, Oh My!
Containers, Virtual Machines, and Bare Metal, Oh My!Containers, Virtual Machines, and Bare Metal, Oh My!
Containers, Virtual Machines, and Bare Metal, Oh My!
 
Chef Vault: A Deep Dive
Chef Vault: A Deep DiveChef Vault: A Deep Dive
Chef Vault: A Deep Dive
 
Open Source Governance 101
Open Source Governance 101Open Source Governance 101
Open Source Governance 101
 
DevOps in Politics
DevOps in PoliticsDevOps in Politics
DevOps in Politics
 
Open Source Governance - The Hard Parts
Open Source Governance - The Hard PartsOpen Source Governance - The Hard Parts
Open Source Governance - The Hard Parts
 
Creating Packages that Run Anywhere with Chef Habitat
Creating Packages that Run Anywhere with Chef HabitatCreating Packages that Run Anywhere with Chef Habitat
Creating Packages that Run Anywhere with Chef Habitat
 
Behind the curtain
Behind the curtainBehind the curtain
Behind the curtain
 

Regular Expressions Guide