SlideShare a Scribd company logo
/regex will find me/Regular Expressions
What are Regular ExpressionsRegular 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/ =~ category15 % off>> /pants/ =~ category=> nil>> /shoes/ =~ category=> 0>> category = "women's shoes”>> /shoes/ =~ category=> 88th character
Scan>> numbers = "one two three"=> "one two three">> numbers.scan(/\w+/)=> ["one", "two", "three”]
Split with Regular Expressions>> "one two\tthree".split(/\s/)=> ["one", ”two", "three"]
gsub”fred,mary,john".gsub(/fred/, “XXX”)=> “XXX,mary,john”
gsub with a block"one two\tthree".gsub(/(\w+)/) do |w| 	puts wendonetwothree
Title CaseCapitalize All Words of a Sentence:>> full_name.gsub(/\b\w/){|s| s.upcase}=> "Yukihiro Matsumoto"

More Related Content

PDF
Regular expressions in Ruby and Introduction to Vim
Stalin Thangaraj
 
PPT
Les08
Sudharsan S
 
KEY
Andrei's Regex Clinic
Andrei Zmievski
 
PDF
Java -lec-3
Zubair Khalid
 
PPTX
Regular expressions
Ивелин Кирилов
 
PPT
Introduction to Regular Expressions
Jesse Anderson
 
PPT
The Power of Regular Expression: use in notepad++
Anjesh Tuladhar
 
PPT
Regular Expressions 2007
Geoffrey Dunn
 
Regular expressions in Ruby and Introduction to Vim
Stalin Thangaraj
 
Andrei's Regex Clinic
Andrei Zmievski
 
Java -lec-3
Zubair Khalid
 
Regular expressions
Ивелин Кирилов
 
Introduction to Regular Expressions
Jesse Anderson
 
The Power of Regular Expression: use in notepad++
Anjesh Tuladhar
 
Regular Expressions 2007
Geoffrey Dunn
 

What's hot (8)

PPT
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
PPT
16 Java Regex
wayn
 
PPT
Textpad and Regular Expressions
OCSI
 
PPTX
Regex lecture
Jun Shimizu
 
PPTX
Regular expressions
Thomas Langston
 
PPTX
LISP: Type specifiers in lisp
DataminingTools Inc
 
PDF
Chapter 7 expressions and assignment statements ii
allyn joy calcaben
 
PPTX
Regular Expressions
primeteacher32
 
Bioinformatica 06-10-2011-p2 introduction
Prof. Wim Van Criekinge
 
16 Java Regex
wayn
 
Textpad and Regular Expressions
OCSI
 
Regex lecture
Jun Shimizu
 
Regular expressions
Thomas Langston
 
LISP: Type specifiers in lisp
DataminingTools Inc
 
Chapter 7 expressions and assignment statements ii
allyn joy calcaben
 
Regular Expressions
primeteacher32
 
Ad

Viewers also liked (10)

PPT
Hortalizas
Ruben Lupaca
 
PDF
Diplomarbeit Vitus LM
derDoc
 
PPT
Actividad 2
Migdanys
 
PDF
An agro economic performance of maize under different weeding regimes
Alexander Decker
 
PPS
Framwroks De Testes
Diego Pacheco
 
PPT
Snc Segona Sessio
joanbosca3b
 
PPT
You and media
D Murali ☆
 
PPS
Finaldefesta
guest80f4785
 
DOC
Cartel Dia De La Familia
PASTORAL FAMILIAR DIOCESIS MATAMORES
 
PDF
Abnormalities of hormones and inflammatory cytokines in women affected with p...
Alexander Decker
 
Hortalizas
Ruben Lupaca
 
Diplomarbeit Vitus LM
derDoc
 
Actividad 2
Migdanys
 
An agro economic performance of maize under different weeding regimes
Alexander Decker
 
Framwroks De Testes
Diego Pacheco
 
Snc Segona Sessio
joanbosca3b
 
You and media
D Murali ☆
 
Finaldefesta
guest80f4785
 
Cartel Dia De La Familia
PASTORAL FAMILIAR DIOCESIS MATAMORES
 
Abnormalities of hormones and inflammatory cytokines in women affected with p...
Alexander Decker
 
Ad

Similar to Ruby RegEx (20)

KEY
Reg EX
Blazing Cloud
 
PPT
Php String And Regular Expressions
mussawir20
 
PPT
Regular expressions
Raj Gupta
 
PPT
Regular Expressions
Satya Narayana
 
PPTX
4 Regex Enumerables
liahhansen
 
PPTX
Javascript正则表达式
ji guang
 
PDF
3.2 javascript regex
Jalpesh Vasa
 
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
PPT
Regex Basics
Jeremy Coates
 
PPT
Regex Cards - Powerpoint Format
Adam Lowe
 
PPT
Introduction to regular expressions
Ben Brumfield
 
ODP
Regex Presentation
arnolambert
 
ODP
Regex Presentation
arnolambert
 
PDF
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
PPSX
Regular expressions in oracle
Logan Palanisamy
 
PDF
Regular Expression Cheat Sheet
SydneyJohnson57
 
PDF
Basta mastering regex power
Max Kleiner
 
PPTX
Regular_Expressions.pptx
DurgaNayak4
 
PPT
Php Chapter 4 Training
Chris Chubb
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Php String And Regular Expressions
mussawir20
 
Regular expressions
Raj Gupta
 
Regular Expressions
Satya Narayana
 
4 Regex Enumerables
liahhansen
 
Javascript正则表达式
ji guang
 
3.2 javascript regex
Jalpesh Vasa
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
Regex Basics
Jeremy Coates
 
Regex Cards - Powerpoint Format
Adam Lowe
 
Introduction to regular expressions
Ben Brumfield
 
Regex Presentation
arnolambert
 
Regex Presentation
arnolambert
 
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
Regular expressions in oracle
Logan Palanisamy
 
Regular Expression Cheat Sheet
SydneyJohnson57
 
Basta mastering regex power
Max Kleiner
 
Regular_Expressions.pptx
DurgaNayak4
 
Php Chapter 4 Training
Chris Chubb
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 

More from Sarah Allen (20)

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

Ruby RegEx

  • 1. /regex will find me/Regular Expressions
  • 2. What are Regular ExpressionsRegular 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/)
  • 6. They are escaped with a backward slash (\).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/ =~ category15 % off>> /pants/ =~ category=> nil>> /shoes/ =~ category=> 0>> category = "women's shoes”>> /shoes/ =~ category=> 88th character
  • 12. Scan>> numbers = "one two three"=> "one two three">> numbers.scan(/\w+/)=> ["one", "two", "three”]
  • 13. Split with Regular Expressions>> "one two\tthree".split(/\s/)=> ["one", ”two", "three"]
  • 15. gsub with a block"one two\tthree".gsub(/(\w+)/) do |w| puts wendonetwothree
  • 16. Title CaseCapitalize All Words of a Sentence:>> full_name.gsub(/\b\w/){|s| s.upcase}=> "Yukihiro Matsumoto"

Editor's Notes

  • #14: s finds spaces, tabs and new lines