SlideShare ist ein Scribd-Unternehmen logo
1 von 124
BEYOND THE BASICS
                           Regular Expressions in Ruby

                                 @nellshamrell




Thursday, February 7, 13
^4[0-9]{12}(?:[0-9]{3})?$



                           Source: regular-expressions.info

Thursday, February 7, 13
Regular Expressions are Patterns




Thursday, February 7, 13
Test

                       Extract

                       Change


Thursday, February 7, 13
Test

                           Extract

                       Change


Thursday, February 7, 13
Test

                           Extract

                       Change


Thursday, February 7, 13
In Ruby, regular
                           expressions are objects




Thursday, February 7, 13
You program from a regular
                        expression to a result



                               Source: The Well Grounded Rubyist

Thursday, February 7, 13
Oniguruma




Thursday, February 7, 13
Shorthand for Hexadecimals

                              /h and /H




Thursday, February 7, 13
Onigmo




Thursday, February 7, 13
Thursday, February 7, 13
=~

Thursday, February 7, 13
/force/ =~ “Use the force”




Thursday, February 7, 13
“Use the force” =~ /force/




Thursday, February 7, 13
“Use the force” =~ /force/

        => 8




Thursday, February 7, 13
/dark side/ !~ “Use the force”




Thursday, February 7, 13
/dark side/ !~ “Use the force”

        => true




Thursday, February 7, 13
MatchData




Thursday, February 7, 13
.match




Thursday, February 7, 13
string = “The force will be with
                           you always.”




Thursday, February 7, 13
string = “The force will be with
                           you always.”
           m =/force/.match(string)




Thursday, February 7, 13
string = “The force will be with
                           you always.”
           m =/force/.match(string)

            => #<MatchData “force” >



Thursday, February 7, 13
string = “The force will be with
                           you always.”
           m =/force/.match(string,5)




Thursday, February 7, 13
string = “The force will be with
                           you always.”
           m =/force/.match(string,5)

            => nil



Thursday, February 7, 13
What can you do with MatchData?




Thursday, February 7, 13
m.to_s




Thursday, February 7, 13
m.to_s
       => “force”




Thursday, February 7, 13
m.to_s
       => “force”

       m.pre_match




Thursday, February 7, 13
m.to_s
       => “force”

       m.pre_match
       => “The ”




Thursday, February 7, 13
m.to_s
       => “force”

       m.pre_match
       => “The ”

       m.post_match

Thursday, February 7, 13
m.to_s
       => “force”

       m.pre_match
       => “The ”

       m.post_match
       => “ be with you”
Thursday, February 7, 13
Capture Groups




Thursday, February 7, 13
/(.*)force(.*)/




Thursday, February 7, 13
m = /(.*)force(.*)/.match(string)




Thursday, February 7, 13
m = /(.*)force(.*)/.match(string)

           m.captures




Thursday, February 7, 13
m = /(.*)force(.*)/.match(string)

           m.captures

           => [“The ”,

                           “will be with you always”]

Thursday, February 7, 13
You access capture groups with []




Thursday, February 7, 13
m[1]




Thursday, February 7, 13
m[1]
       => “The ”




Thursday, February 7, 13
m[1]
       => “The ”

       m[2]




Thursday, February 7, 13
m[1]
       => “The ”

       m[2]
       => “ will be with you always ”




Thursday, February 7, 13
m[0]




Thursday, February 7, 13
m[0]
       => “The force will be with
          you always.”




Thursday, February 7, 13
Match Objects are not arrays




Thursday, February 7, 13
m.each do |match|
             puts match.upcase
           end




Thursday, February 7, 13
m.each do |match|
             puts match.upcase
           end
           => NoMethodError




Thursday, February 7, 13
m.to_a.each do |match|
             puts match.upcase
           end




Thursday, February 7, 13
m.to_a.each do |match|
             puts match.upcase
           end
           => “THE FORCE WILL BE
              WITH YOU ALWAYS”
                           “THE ”
                           “WILL BE WITH YOU
                           ALWAYS”
Thursday, February 7, 13
Thursday, February 7, 13
LookArounds Define Context




Thursday, February 7, 13
string = “Who’s the more
                      foolish? The fool or
                      the fool who follows
                      him?”




Thursday, February 7, 13
string = “Who’s the more
                      foolish? The fool or
                      the fool who follows
                      him?”

             /fool/



Thursday, February 7, 13
string.scan(/fool/)




Thursday, February 7, 13
string.scan(/fool/)

          => [“fool”, “fool”, “fool”]




Thursday, February 7, 13
Positive Lookahead




Thursday, February 7, 13
?=




Thursday, February 7, 13
string.scan(/fool(?=ish)/)




Thursday, February 7, 13
string.scan(/fool(?=ish)/)

          => [“fool”]




Thursday, February 7, 13
string.gsub(/fool(?=ish)/, “self”)




Thursday, February 7, 13
string.gsub(/fool(?=ish)/, “self”)

          => “Who’s the more
                           selfish? The fool or
                           the fool who follows
                           him?”



Thursday, February 7, 13
Zero Width Positive
                           Lookahead Assertion




Thursday, February 7, 13
Zero width means it does not
                         consume characters




Thursday, February 7, 13
Positive means a match for the
                 lookahead should be present




Thursday, February 7, 13
Lookahead means it is looking
                   ahead of your main match.




Thursday, February 7, 13
Assertion means the lookahead only
    determines whether a match exists




Thursday, February 7, 13
string = “Who’s the more
                      foolish? The fool or
                      the fool who follows
                      him?”




Thursday, February 7, 13
Negative Lookahead




Thursday, February 7, 13
Negative means a match for the
         lookahead should not be present




Thursday, February 7, 13
?!




Thursday, February 7, 13
string.scan(/fool(?!ish)/)




Thursday, February 7, 13
string.scan(/fool(?!ish)/)

          => [“fool”, “fool”]




Thursday, February 7, 13
string.gsub(/fool(?!ish)/, “self”)




Thursday, February 7, 13
string.gsub(/fool(?!ish)/, “self”)

          => “Who’s the more
                           foolish? The self or
                           the self who follows
                           him?”



Thursday, February 7, 13
Positive Lookbehind




Thursday, February 7, 13
string = “For my ally is the
                      force, and a powerful
                      ally it is”




Thursday, February 7, 13
string = “For my ally is the
                      force, and a powerful
                      ally it is”


             /ally/



Thursday, February 7, 13
?<=




Thursday, February 7, 13
/(?<=powerful )ally/




Thursday, February 7, 13
string.gsub(/(?<=powerful )ally/,
        “friend”)




Thursday, February 7, 13
string.gsub(/(?<=powerful )ally/,
        “friend”)
        => “For my ally is the
                           force, and a powerful
                           friend it is”




Thursday, February 7, 13
Negative Lookbehind




Thursday, February 7, 13
?<!




Thursday, February 7, 13
/(?<!powerful )ally/




Thursday, February 7, 13
string.gsub(/(?<!powerful )ally/,
        “friend”)




Thursday, February 7, 13
string.gsub(/(?<!powerful )ally/,
        “friend”)
        => “For my friend is the
                           force, and a powerful
                           ally it is”




Thursday, February 7, 13
Thursday, February 7, 13
Regular Expressions have
                              distinct behaviors




Thursday, February 7, 13
Greedy

                           Lazy

                       Possessive


Thursday, February 7, 13
Greedy

                           Lazy

                       Possessive


Thursday, February 7, 13
Greedy

                           Lazy

                           Possessive


Thursday, February 7, 13
Quantifiers




Thursday, February 7, 13
+


Thursday, February 7, 13
+
                           /.+/

Thursday, February 7, 13
Quantifiers are greedy by default




Thursday, February 7, 13
Greedy Quantifiers match
                             as much as possible




Thursday, February 7, 13
Greedy Quantifiers use maximum
         effort for maximum return




Thursday, February 7, 13
string = “This is no time to
                      talk about time we
                      don’t have the time”




Thursday, February 7, 13
string = “This is no time to
                      talk about time we
                      don’t have the time”


             /.+time/



Thursday, February 7, 13
/.+time/.match(string)




Thursday, February 7, 13
/.+time/.match(string)

          => “This is no time to
                           talk about time we
                           don’t have the time”




Thursday, February 7, 13
Greedy regular expressions
                             try to match the whole
                            string, then backtrack




Thursday, February 7, 13
Oniguruma makes
                           backtracking quicker




Thursday, February 7, 13
Lazy Quantifiers




Thursday, February 7, 13
Lazy Quantifiers match
                            as little as possible




Thursday, February 7, 13
Lazy Quantifiers use minimum
              effort for minimum return




Thursday, February 7, 13
/.+?time/




Thursday, February 7, 13
/.+?time/.match(string)




Thursday, February 7, 13
/.+?time/.match(string)

          => “This is no time”




Thursday, February 7, 13
Lazy regular expressions
                              use less resources




Thursday, February 7, 13
Possessive Quantifiers




Thursday, February 7, 13
Possessive Quantifiers are
                                all or nothing




Thursday, February 7, 13
Possessive Quantifiers try to
                        match the entire string
                       with no backtracking




Thursday, February 7, 13
Possessive Quantifiers use
                             minimum effort for
                              maximum return




Thursday, February 7, 13
/.++time/




Thursday, February 7, 13
/.++time/.match(string)




Thursday, February 7, 13
/.++time/.match(string)

          => nil




Thursday, February 7, 13
Possessive Quantifiers
                                fail faster




Thursday, February 7, 13
Thursday, February 7, 13
Write regular expressions
                               in small chunks




Thursday, February 7, 13
Rubular




Thursday, February 7, 13
Regular expressions come in drafts




Thursday, February 7, 13
Move beyond your fear
                           of regular expressions




Thursday, February 7, 13
Nell Shamrell
             Software Development Engineer
                           Blue Box Group
                           @nellshamrell


Thursday, February 7, 13

Weitere ähnliche Inhalte

Mehr von Nell Shamrell-Harrington

Mehr von Nell Shamrell-Harrington (20)

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
 
Refactoring terraform
Refactoring terraformRefactoring terraform
Refactoring terraform
 
Refactoring Infrastructure Code
Refactoring Infrastructure CodeRefactoring Infrastructure Code
Refactoring Infrastructure Code
 
Devops: A History
Devops: A HistoryDevops: A History
Devops: A History
 
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)
 
First Do No Harm: Surgical Refactoring
First Do No Harm: Surgical RefactoringFirst Do No Harm: Surgical Refactoring
First Do No Harm: Surgical Refactoring
 
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
 
Public Supermarket: The Insider's Tour
Public Supermarket: The Insider's TourPublic Supermarket: The Insider's Tour
Public Supermarket: The Insider's Tour
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Beyond the Basics: Regular Expressions in Ruby