SlideShare a Scribd company logo
1 of 16
/regex will find me/ Regular Expressions
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 ,[object Object]
 They are escaped with a backward slash (.,[object Object]
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   Start of string   End of string
Regex Basics cont... . Any single character   Any whitespace character   Any non-whitespace character   Any digit   Any non-digit   Any word character (letter, number, underscore)   Any non-word character   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(/+/) => ["one", "two", "three”]
Split with Regular Expressions >> "one twothree".split(//) => ["one", ”two", "three"]
gsub ”fred,mary,john".gsub(/fred/, “XXX”) => “XXX,mary,john”
gsub with a block "one twothree".gsub(/(+)/) do |w| 	puts w end one two three
Title Case Capitalize All Words of a Sentence: >> full_name.gsub(//){|s| s.upcase} => "Yukihiro Matsumoto"

More Related Content

What's hot

Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regexwayn
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular ExpressionsOCSI
 
Chapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements iiChapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements iiallyn joy calcaben
 

What's hot (8)

Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular Expressions
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
LISP: Type specifiers in lisp
LISP: Type specifiers in lispLISP: Type specifiers in lisp
LISP: Type specifiers in lisp
 
Chapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements iiChapter 7 expressions and assignment statements ii
Chapter 7 expressions and assignment statements ii
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 

Viewers also liked

Diplomarbeit Vitus LM
Diplomarbeit Vitus LMDiplomarbeit Vitus LM
Diplomarbeit Vitus LMderDoc
 
Actividad 2
Actividad 2Actividad 2
Actividad 2Migdanys
 
An agro economic performance of maize under different weeding regimes
An agro economic performance of maize under different weeding regimesAn agro economic performance of maize under different weeding regimes
An agro economic performance of maize under different weeding regimesAlexander Decker
 
Snc Segona Sessio
Snc Segona SessioSnc Segona Sessio
Snc Segona Sessiojoanbosca3b
 
Abnormalities of hormones and inflammatory cytokines in women affected with p...
Abnormalities of hormones and inflammatory cytokines in women affected with p...Abnormalities of hormones and inflammatory cytokines in women affected with p...
Abnormalities of hormones and inflammatory cytokines in women affected with p...Alexander Decker
 

Viewers also liked (10)

Hortalizas
HortalizasHortalizas
Hortalizas
 
Diplomarbeit Vitus LM
Diplomarbeit Vitus LMDiplomarbeit Vitus LM
Diplomarbeit Vitus LM
 
Actividad 2
Actividad 2Actividad 2
Actividad 2
 
An agro economic performance of maize under different weeding regimes
An agro economic performance of maize under different weeding regimesAn agro economic performance of maize under different weeding regimes
An agro economic performance of maize under different weeding regimes
 
Framwroks De Testes
Framwroks De  TestesFramwroks De  Testes
Framwroks De Testes
 
Snc Segona Sessio
Snc Segona SessioSnc Segona Sessio
Snc Segona Sessio
 
You and media
You and mediaYou and media
You and media
 
Finaldefesta
FinaldefestaFinaldefesta
Finaldefesta
 
Cartel Dia De La Familia
Cartel Dia De La FamiliaCartel Dia De La Familia
Cartel Dia De La Familia
 
Abnormalities of hormones and inflammatory cytokines in women affected with p...
Abnormalities of hormones and inflammatory cytokines in women affected with p...Abnormalities of hormones and inflammatory cytokines in women affected with p...
Abnormalities of hormones and inflammatory cytokines in women affected with p...
 

Similar to Ruby RegEx

Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressionsmussawir20
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
4 Regex Enumerables
4 Regex Enumerables4 Regex Enumerables
4 Regex Enumerablesliahhansen
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式ji guang
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfBryan Alejos
 
Regex Cards - Powerpoint Format
Regex Cards - Powerpoint FormatRegex Cards - Powerpoint Format
Regex Cards - Powerpoint FormatAdam Lowe
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressionsBen Brumfield
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracleLogan Palanisamy
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat SheetSydneyJohnson57
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex powerMax Kleiner
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptxDurgaNayak4
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 TrainingChris Chubb
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions WebStackAcademy
 

Similar to Ruby RegEx (20)

Reg EX
Reg EXReg EX
Reg EX
 
Php String And Regular Expressions
Php String  And Regular ExpressionsPhp String  And Regular Expressions
Php String And Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
4 Regex Enumerables
4 Regex Enumerables4 Regex Enumerables
4 Regex Enumerables
 
Javascript正则表达式
Javascript正则表达式Javascript正则表达式
Javascript正则表达式
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 
Regex Basics
Regex BasicsRegex Basics
Regex Basics
 
Regex Cards - Powerpoint Format
Regex Cards - Powerpoint FormatRegex Cards - Powerpoint Format
Regex Cards - Powerpoint Format
 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex2013 - Andrei Zmievski: Clínica Regex
2013 - Andrei Zmievski: Clínica Regex
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Regular Expression Cheat Sheet
Regular Expression Cheat SheetRegular Expression Cheat Sheet
Regular Expression Cheat Sheet
 
Basta mastering regex power
Basta mastering regex powerBasta mastering regex power
Basta mastering regex power
 
Regular_Expressions.pptx
Regular_Expressions.pptxRegular_Expressions.pptx
Regular_Expressions.pptx
 
Php Chapter 4 Training
Php Chapter 4 TrainingPhp Chapter 4 Training
Php Chapter 4 Training
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 JavaScript - Chapter 9 - TypeConversion and Regular Expressions  JavaScript - Chapter 9 - TypeConversion and Regular Expressions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
 

More from Sarah Allen

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesSarah Allen
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)Sarah Allen
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical SkillSarah Allen
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government ServicesSarah Allen
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency WinsSarah Allen
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of ComputersSarah Allen
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software FunSarah Allen
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of TransparencySarah Allen
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for FunSarah Allen
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceSarah Allen
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead PeopleSarah Allen
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby Sarah Allen
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Sarah Allen
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?Sarah Allen
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersSarah Allen
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentSarah Allen
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeSarah Allen
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future ThoughtsSarah Allen
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web VideoSarah Allen
 

More from Sarah Allen (20)

Internet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequencesInternet security: a landscape of unintended consequences
Internet security: a landscape of unintended consequences
 
RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)RTMP: how did we get to now? (Demuxed 2019)
RTMP: how did we get to now? (Demuxed 2019)
 
Communication is a Technical Skill
Communication is a Technical SkillCommunication is a Technical Skill
Communication is a Technical Skill
 
Improving Federal Government Services
Improving Federal Government ServicesImproving Federal Government Services
Improving Federal Government Services
 
Transparency Wins
Transparency WinsTransparency Wins
Transparency Wins
 
A Short History of Computers
A Short History of ComputersA Short History of Computers
A Short History of Computers
 
Making Software Fun
Making Software FunMaking Software Fun
Making Software Fun
 
Power of Transparency
Power of TransparencyPower of Transparency
Power of Transparency
 
Designing for Fun
Designing for FunDesigning for Fun
Designing for Fun
 
Ruby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World ConferenceRuby in the US Government for Ruby World Conference
Ruby in the US Government for Ruby World Conference
 
Identities of Dead People
Identities of Dead PeopleIdentities of Dead People
Identities of Dead People
 
Let's pretend
Let's pretendLet's pretend
Let's pretend
 
3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby 3 Reasons Not to Use Ruby
3 Reasons Not to Use Ruby
 
Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?Ruby Nation: Why no haz Ruby?
Ruby Nation: Why no haz Ruby?
 
Why no ruby in gov?
Why no ruby in gov?Why no ruby in gov?
Why no ruby in gov?
 
People Patterns or What I learned from Toastmasters
People Patterns or What I learned from ToastmastersPeople Patterns or What I learned from Toastmasters
People Patterns or What I learned from Toastmasters
 
Blazing Cloud: Agile Product Development
Blazing Cloud: Agile Product DevelopmentBlazing Cloud: Agile Product Development
Blazing Cloud: Agile Product Development
 
Crowdsourced Transcription Landscape
Crowdsourced Transcription LandscapeCrowdsourced Transcription Landscape
Crowdsourced Transcription Landscape
 
Lessons Learned Future Thoughts
Lessons Learned Future ThoughtsLessons Learned Future Thoughts
Lessons Learned Future Thoughts
 
Mobile Web Video
Mobile Web VideoMobile Web Video
Mobile Web Video
 

Ruby RegEx

  • 1. /regex will find me/ Regular Expressions
  • 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.
  • 6.
  • 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 Start of string End of string
  • 8. Regex Basics cont... . Any single character Any whitespace character Any non-whitespace character Any digit Any non-digit Any word character (letter, number, underscore) Any non-word character 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(/+/) => ["one", "two", "three”]
  • 13. Split with Regular Expressions >> "one twothree".split(//) => ["one", ”two", "three"]
  • 15. gsub with a block "one twothree".gsub(/(+)/) do |w| puts w end one two three
  • 16. Title Case Capitalize All Words of a Sentence: >> full_name.gsub(//){|s| s.upcase} => "Yukihiro Matsumoto"

Editor's Notes

  1. s finds spaces, tabs and new lines