SlideShare a Scribd company logo
REGULAR EXPRESSIONS
      /regex will find me/
WHAT ARE REGULAR
            EXPRESSIONS
• Regular expressions allow matching and manipulation of textual
  data.
• Abbreviated as regex or regexp, or alternatively, just patterns
REGULAR EXPRESSIONS IN
        RUBY
USING REGULAR
                 EXPRESSIONS
• Scan a string for multiple occurrences of a pattern.
• Replace part of a string with another string.
• Split a string based on a matching separator.
REGULAR EXPRESSION
        SYNTAX
• Regular expressions are put between two forward
  slashes (/match_me/)

• They are escaped with a backward slash ().
CHARACTERS THAT NEED TO BE
        ESCAPED
 . | ( ) [ ] { } +  ^ $ * ?
REGEX BASICS

• [abc] A single character: a, b or c
• [^abc] Any single character but a, b, or c
• [a-z] Any single character in the range a-z
• [a-zA-Z] Any single character in the range a-z or A-Z
• ^ Start of line
• $ End of line
• A Start of string
• z End of string
REGEX BASICS CONT...

• . Any single character
• s Any whitespace character
• S Any non-whitespace character
• d Any digit
• D Any non-digit
• w Any word character (letter, number, underscore)
• W Any non-word character
• b Any word boundary character
REGEX BASICS CONT...

•   (...) Capture everything enclosed
•   (a|b) a or b
•   a? Zero or one of a
•   a* Zero or more of a
•   a+ One or more of a
•   a{3} Exactly 3 of a
•   a{3,} 3 or more of a
•    a{3,6} Between 3 and 6 of a
REGEX: .MATCH
>> category = "power tools"
=> "power tools"

>> puts "on Sale" if category.match(/power tools/)
on Sale

>> puts "on Sale" if /power tools/.match(category)
on Sale
REGEX: =~
>> category = "shoes"
=> "shoes"

>> puts "15 % off" if category =~ /shoes/
15 % off

>> puts "15 % off" if /shoes/ =~ category
15 % off

>> /pants/ =~ category
=> nil

>> /shoes/ =~ category
=> 0

>> category = "women's shoes”

>> /shoes/ =~ category
=> 8                                8th character
SCAN

>> numbers = "one two three"
=> "one two three"
>> numbers.scan(/w+/)
=> ["one", "two", "three”]
SPLIT WITH REGULAR
       EXPRESSIONS
>> "one twotthree".split(/s/)
=> ["one", ”two", "three"]
GSUB

”fred,mary,john".gsub(/fred/, “XXX”)
=> “XXX,mary,john”
GSUB WITH A BLOCK

"one twotthree".gsub(/(w+)/) do |w| 
 puts w
end


one
two
three
TITLE CASE
Capitalize All Words of a Sentence:


>> full_name.gsub(/bw/){|s| s.upcase}
=> "Yukihiro Matsumoto"

More Related Content

PPTX
Regular expressions
Brij Kishore
 
ODP
Regular Expression
Lambert Lum
 
KEY
Regular Expressions 101
Raj Rajandran
 
PPTX
Introduction to Regular Expressions
Matt Casto
 
PPTX
Regular expressions
Ignaz Wanders
 
PPT
Regular Expressions
Satya Narayana
 
KEY
Andrei's Regex Clinic
Andrei Zmievski
 
PPT
Textpad and Regular Expressions
OCSI
 
Regular expressions
Brij Kishore
 
Regular Expression
Lambert Lum
 
Regular Expressions 101
Raj Rajandran
 
Introduction to Regular Expressions
Matt Casto
 
Regular expressions
Ignaz Wanders
 
Regular Expressions
Satya Narayana
 
Andrei's Regex Clinic
Andrei Zmievski
 
Textpad and Regular Expressions
OCSI
 

Similar to Reg EX (20)

PPTX
Ruby RegEx
Sarah Allen
 
PPTX
4 Regex Enumerables
liahhansen
 
KEY
Regular expressions
James Gray
 
PDF
Regular expressions in Ruby and Introduction to Vim
Stalin Thangaraj
 
ODP
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Kuyseng Chhoeun
 
PDF
How to check valid Email? Find using regex.
Poznań Ruby User Group
 
PDF
Regex - Regular Expression Basics
Eterna Han Tsai
 
PDF
And Now You Have Two Problems
Luca Mearelli
 
PDF
And now you have two problems. Ruby regular expressions for fun and profit by...
Codemotion
 
PDF
How to check valid Email? Find using regex.
Poznań Ruby User Group
 
ODP
Looking for Patterns
Keith Wright
 
PPTX
Regular Expressions Boot Camp
Chris Schiffhauer
 
PPT
Regular Expressions 2007
Geoffrey Dunn
 
PPTX
Regular Expressions
Akhil Kaushik
 
PPT
2.regular expressions
Praveen Gorantla
 
PPTX
Regular Expression Crash Course
Imran Qasim
 
PPT
regular-expressions lecture 28-string regular expression
smallboss311
 
PDF
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
PDF
3.2 javascript regex
Jalpesh Vasa
 
PPTX
NLP_KASHK:Regular Expressions
Hemantha Kulathilake
 
Ruby RegEx
Sarah Allen
 
4 Regex Enumerables
liahhansen
 
Regular expressions
James Gray
 
Regular expressions in Ruby and Introduction to Vim
Stalin Thangaraj
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Kuyseng Chhoeun
 
How to check valid Email? Find using regex.
Poznań Ruby User Group
 
Regex - Regular Expression Basics
Eterna Han Tsai
 
And Now You Have Two Problems
Luca Mearelli
 
And now you have two problems. Ruby regular expressions for fun and profit by...
Codemotion
 
How to check valid Email? Find using regex.
Poznań Ruby User Group
 
Looking for Patterns
Keith Wright
 
Regular Expressions Boot Camp
Chris Schiffhauer
 
Regular Expressions 2007
Geoffrey Dunn
 
Regular Expressions
Akhil Kaushik
 
2.regular expressions
Praveen Gorantla
 
Regular Expression Crash Course
Imran Qasim
 
regular-expressions lecture 28-string regular expression
smallboss311
 
/Regex makes me want to (weep|give up|(╯°□°)╯︵ ┻━┻)\.?/i
brettflorio
 
3.2 javascript regex
Jalpesh Vasa
 
NLP_KASHK:Regular Expressions
Hemantha Kulathilake
 
Ad

More from Blazing Cloud (20)

PDF
Rails ORM De-mystifying Active Record has_many
Blazing Cloud
 
PDF
Active Record Introduction - 3
Blazing Cloud
 
PDF
Rails Class Intro - 1
Blazing Cloud
 
PDF
Your first rails app - 2
Blazing Cloud
 
PDF
RSpec Quick Reference
Blazing Cloud
 
PDF
Extending rails
Blazing Cloud
 
KEY
2day Ruby Class Intro
Blazing Cloud
 
KEY
Mobile Lean UX
Blazing Cloud
 
KEY
Interactive Graphics
Blazing Cloud
 
KEY
Interactive Graphics w/ Javascript, HTML5 and CSS3
Blazing Cloud
 
KEY
Form helpers
Blazing Cloud
 
KEY
Intro to Ruby (and RSpec)
Blazing Cloud
 
KEY
What you don't know (yet)
Blazing Cloud
 
KEY
Introduction to Rails
Blazing Cloud
 
KEY
ActiveRecord
Blazing Cloud
 
KEY
Ruby on Rails Class intro
Blazing Cloud
 
KEY
Ruby on rails toolbox
Blazing Cloud
 
KEY
Routes Controllers
Blazing Cloud
 
KEY
Test Driven Development
Blazing Cloud
 
KEY
Active Record
Blazing Cloud
 
Rails ORM De-mystifying Active Record has_many
Blazing Cloud
 
Active Record Introduction - 3
Blazing Cloud
 
Rails Class Intro - 1
Blazing Cloud
 
Your first rails app - 2
Blazing Cloud
 
RSpec Quick Reference
Blazing Cloud
 
Extending rails
Blazing Cloud
 
2day Ruby Class Intro
Blazing Cloud
 
Mobile Lean UX
Blazing Cloud
 
Interactive Graphics
Blazing Cloud
 
Interactive Graphics w/ Javascript, HTML5 and CSS3
Blazing Cloud
 
Form helpers
Blazing Cloud
 
Intro to Ruby (and RSpec)
Blazing Cloud
 
What you don't know (yet)
Blazing Cloud
 
Introduction to Rails
Blazing Cloud
 
ActiveRecord
Blazing Cloud
 
Ruby on Rails Class intro
Blazing Cloud
 
Ruby on rails toolbox
Blazing Cloud
 
Routes Controllers
Blazing Cloud
 
Test Driven Development
Blazing Cloud
 
Active Record
Blazing Cloud
 
Ad

Recently uploaded (20)

PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
The Future of Artificial Intelligence (AI)
Mukul
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 

Reg EX

  • 1. REGULAR EXPRESSIONS /regex will find me/
  • 2. WHAT ARE REGULAR EXPRESSIONS • Regular expressions allow matching and manipulation of textual data. • Abbreviated as regex or regexp, or alternatively, just patterns
  • 4. USING REGULAR EXPRESSIONS • Scan a string for multiple occurrences of a pattern. • Replace part of a string with another string. • Split a string based on a matching separator.
  • 5. REGULAR EXPRESSION SYNTAX • Regular expressions are put between two forward slashes (/match_me/) • They are escaped with a backward slash ().
  • 6. CHARACTERS THAT NEED TO BE ESCAPED . | ( ) [ ] { } + ^ $ * ?
  • 7. REGEX BASICS • [abc] A single character: a, b or c • [^abc] Any single character but a, b, or c • [a-z] Any single character in the range a-z • [a-zA-Z] Any single character in the range a-z or A-Z • ^ Start of line • $ End of line • A Start of string • z End of string
  • 8. REGEX BASICS CONT... • . Any single character • s Any whitespace character • S Any non-whitespace character • d Any digit • D Any non-digit • w Any word character (letter, number, underscore) • W Any non-word character • b Any word boundary character
  • 9. REGEX BASICS CONT... • (...) Capture everything enclosed • (a|b) a or b • a? Zero or one of a • a* Zero or more of a • a+ One or more of a • a{3} Exactly 3 of a • a{3,} 3 or more of a • a{3,6} Between 3 and 6 of a
  • 10. REGEX: .MATCH >> category = "power tools" => "power tools" >> puts "on Sale" if category.match(/power tools/) on Sale >> puts "on Sale" if /power tools/.match(category) on Sale
  • 11. REGEX: =~ >> category = "shoes" => "shoes" >> puts "15 % off" if category =~ /shoes/ 15 % off >> puts "15 % off" if /shoes/ =~ category 15 % off >> /pants/ =~ category => nil >> /shoes/ =~ category => 0 >> category = "women's shoes” >> /shoes/ =~ category => 8 8th character
  • 12. SCAN >> numbers = "one two three" => "one two three" >> numbers.scan(/w+/) => ["one", "two", "three”]
  • 13. SPLIT WITH REGULAR EXPRESSIONS >> "one twotthree".split(/s/) => ["one", ”two", "three"]
  • 15. GSUB WITH A BLOCK "one twotthree".gsub(/(w+)/) do |w| puts w end one two three
  • 16. TITLE CASE Capitalize All Words of a Sentence: >> full_name.gsub(/bw/){|s| s.upcase} => "Yukihiro Matsumoto"