SlideShare a Scribd company logo
1 of 24
Download to read offline
Ruby Regular Expression - Basics
Regular Expression
Historically, regular expressions are one
of computer science's shining examples
of how using good theory leads to good
programs.
Today, regular expressions have also
become a shining example of how
ignoring good theory leads to bad
programs.
What is regular expression
●
Is a notation for describing a set of
character strings
●
sequence of characters within /, /
●
single character within /, / is the simple
regular expression
Ex: /hello world/
What can be done with regular
expression ?
●
Test a string to see whether it matches
a pattern
●
Extract from a string the sections that
match all or part of a pattern
●
Change the string, replacing parts that
match a pattern
Metacharcters
●
Some characters have special meaning
in regular expression
– ., |, (, ), [, ], {, }, +, , ^, $, * and ?
–
●
They don't match themselves
●
To match, escape it with ''
Ex: '*' =~ /*/
Ruby Regular Expressions
●
Uses 'Oniguruma' open source C library
by K. Kosako
●
It also an object, Regexp
●
#{...} expression substitutions in the
pattern
How to create pattern in ruby ?
●
//
Ex: /hello/
●
Regexp.new
Ex: Regexp.new('hello')
●
%r{...}
Ex: %r{mm/dd}
Matching against String
“When a particular string is in the set described
by a regular expression, we often say that the
regular expression matches the string”
●
=~
– Returns matched index / nil
●
!~
– Returns true / false
●
match()
– Method of String & Regexp
– Returns MatchData object / nil
Regular expression options
●
i – case insensitive
●
m – multiline mode
●
x – Extended mode
Metacharacters
●
Period ( . )
– To match any character
– Should be escaped to match literal '.'
Metacharacters
●
Anchors
– To restrict the pattern to match beginning
or end of the line
– ^ - beginning of the line
– $ - end of the line
– A – beginning of the string
– Z – end of the string
Metacharacters
●
Character Class
– Set of characters between [ and ]
– Can have ranges
– Can negate character sets
●
Sequences
d, D, w, W, s, S
Ex:
●
/[dr]ejected/
●
Match digits [0-9]
●
Alphanumeric [A-Za-z0-9]
●
Except digits [^0-9]
●
Str.gsub(/['”]/,'')
Metacharacters
●
Repetition
– r* Matches zero or more occurrences of r
– r+ Matches one or more occurrences of r
– r? Matches zero or one occurrence of r
– r{m,n} Matches at least m and at most n
occurrences of r
– r{m,} Matches at least m occurrences of r
– r{,n} Matches at most n occurrences of r
– r{m } Matches exactly m occurrences of r
Metacharacters
●
Alternation
– |
Ex: /red|blue/
●
Grouping
– Everything within the group is treated as a
single regular expression
– Backreferenced by numbers
Metacharacters
●
Repetition
– r* Matches zero or more occurrences of r
– r+ Matches one or more occurrences of r
– r? Matches zero or one occurrence of r
– r{m,n} Matches at least m and at most n
occurrences of r
– r{m,} Matches at least m occurrences of r
– r{,n} Matches at most n occurrences of r
– r{m } Matches exactly m occurrences of r
Vim Editor - Basics
Vim - Intro
●
Vim is a text editor written by Bram
Moolenaar based on 'vi'
●
The original code for 'vi' was written by
Bill Joy in 1976
●
Originally created for Unix systems
Vim – Modes
●
Normal Mode
– characters you type are commands
– Default mode
●
Insert Mode
– the characters are inserted as text
– I, i, A, a, O, o switch to insert mode
– Escp key is to come back to normal mode
Vim – Manipulation
●
Delete
– d
– dw, dd, D, x, X
●
Copy
– y
– yw, yy, Y
●
Join
– J
●
Paste
– P, p
●
Replace
– r, R
●
Undo & Redo
– u, Ctrl+r
Vim – Navigation
●
h, j, k , l
●
w, b, e
●
0, ^, $
– P, p
●
G, gg
●
% (matches {}()[ ])
●
Ctrl-b, Ctrl-f
●
N G ( got to nth line)
Vim – Search & Replace
●
/pattern
●
?pattern
– N, n
●
:range s/oldpattern/new/options
●
F, f, t, T
Vim – Misc
●
Surrounding
●
Swapcase
●
Vim file +pageno
●
Set nu, spell
●
gf
●
autocomplete
Vim – Exit
●
:q
●
:q!
●
:wq
Thanks

More Related Content

What's hot

Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
arnolambert
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
wayn
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
Masudul Haque
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
Anjesh Tuladhar
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
Raj Rajandran
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
Tri Truong
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
Amrit Kaur
 

What's hot (20)

Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
 
16 Java Regex
16 Java Regex16 Java Regex
16 Java Regex
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
 
Textpad and Regular Expressions
Textpad and Regular ExpressionsTextpad and Regular Expressions
Textpad and Regular Expressions
 
Introduction to Regular Expressions
Introduction to Regular ExpressionsIntroduction to Regular Expressions
Introduction to Regular Expressions
 
Regular Expressions 101
Regular Expressions 101Regular Expressions 101
Regular Expressions 101
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Finaal application on regular expression
Finaal application on regular expressionFinaal application on regular expression
Finaal application on regular expression
 
Regular Expressions grep and egrep
Regular Expressions grep and egrepRegular Expressions grep and egrep
Regular Expressions grep and egrep
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
11. using regular expressions with oracle database
11. using regular expressions with oracle database11. using regular expressions with oracle database
11. using regular expressions with oracle database
 
Python - Regular Expressions
Python - Regular ExpressionsPython - Regular Expressions
Python - Regular Expressions
 

Similar to Regular expressions in Ruby and Introduction to Vim

Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Kuyseng Chhoeun
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
Ben Brumfield
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
James Armes
 

Similar to Regular expressions in Ruby and Introduction to Vim (20)

Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekingeBioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
Bioinformatics p2-p3-perl-regexes v2013-wim_vancriekinge
 
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular ExpressionEloquent Ruby chapter 4 - Find The Right String with Regular Expression
Eloquent Ruby chapter 4 - Find The Right String with Regular Expression
 
Bioinformatica p2-p3-introduction
Bioinformatica p2-p3-introductionBioinformatica p2-p3-introduction
Bioinformatica p2-p3-introduction
 
Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013Introduction to Regular Expressions RootsTech 2013
Introduction to Regular Expressions RootsTech 2013
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
Ruby training day1
Ruby training day1Ruby training day1
Ruby training day1
 
Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014Bioinformatics p2-p3-perl-regexes v2014
Bioinformatics p2-p3-perl-regexes v2014
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
lecture2.ppt
lecture2.pptlecture2.ppt
lecture2.ppt
 
Chapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programmingChapter-2 is for tokens in C programming
Chapter-2 is for tokens in C programming
 
Module 3 - Regular Expressions, Dictionaries.pdf
Module 3 - Regular  Expressions,  Dictionaries.pdfModule 3 - Regular  Expressions,  Dictionaries.pdf
Module 3 - Regular Expressions, Dictionaries.pdf
 
Regular Expressions and You
Regular Expressions and YouRegular Expressions and You
Regular Expressions and You
 
Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014Don't Fear the Regex - CapitalCamp/GovDays 2014
Don't Fear the Regex - CapitalCamp/GovDays 2014
 
Regular expressions in oracle
Regular expressions in oracleRegular expressions in oracle
Regular expressions in oracle
 
Linux fundamental - Chap 06 regx
Linux fundamental - Chap 06 regxLinux fundamental - Chap 06 regx
Linux fundamental - Chap 06 regx
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Matching with Regular Expressions
Matching with Regular ExpressionsMatching with Regular Expressions
Matching with Regular Expressions
 
07. haskell Membership
07. haskell Membership07. haskell Membership
07. haskell Membership
 
An introduction to functional programming with Swift
An introduction to functional programming with SwiftAn introduction to functional programming with Swift
An introduction to functional programming with Swift
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Regular expressions in Ruby and Introduction to Vim

  • 2. Regular Expression Historically, regular expressions are one of computer science's shining examples of how using good theory leads to good programs. Today, regular expressions have also become a shining example of how ignoring good theory leads to bad programs.
  • 3. What is regular expression ● Is a notation for describing a set of character strings ● sequence of characters within /, / ● single character within /, / is the simple regular expression Ex: /hello world/
  • 4. What can be done with regular expression ? ● Test a string to see whether it matches a pattern ● Extract from a string the sections that match all or part of a pattern ● Change the string, replacing parts that match a pattern
  • 5. Metacharcters ● Some characters have special meaning in regular expression – ., |, (, ), [, ], {, }, +, , ^, $, * and ? – ● They don't match themselves ● To match, escape it with '' Ex: '*' =~ /*/
  • 6. Ruby Regular Expressions ● Uses 'Oniguruma' open source C library by K. Kosako ● It also an object, Regexp ● #{...} expression substitutions in the pattern
  • 7. How to create pattern in ruby ? ● // Ex: /hello/ ● Regexp.new Ex: Regexp.new('hello') ● %r{...} Ex: %r{mm/dd}
  • 8. Matching against String “When a particular string is in the set described by a regular expression, we often say that the regular expression matches the string” ● =~ – Returns matched index / nil ● !~ – Returns true / false ● match() – Method of String & Regexp – Returns MatchData object / nil
  • 9. Regular expression options ● i – case insensitive ● m – multiline mode ● x – Extended mode
  • 10. Metacharacters ● Period ( . ) – To match any character – Should be escaped to match literal '.'
  • 11. Metacharacters ● Anchors – To restrict the pattern to match beginning or end of the line – ^ - beginning of the line – $ - end of the line – A – beginning of the string – Z – end of the string
  • 12. Metacharacters ● Character Class – Set of characters between [ and ] – Can have ranges – Can negate character sets ● Sequences d, D, w, W, s, S Ex: ● /[dr]ejected/ ● Match digits [0-9] ● Alphanumeric [A-Za-z0-9] ● Except digits [^0-9] ● Str.gsub(/['”]/,'')
  • 13. Metacharacters ● Repetition – r* Matches zero or more occurrences of r – r+ Matches one or more occurrences of r – r? Matches zero or one occurrence of r – r{m,n} Matches at least m and at most n occurrences of r – r{m,} Matches at least m occurrences of r – r{,n} Matches at most n occurrences of r – r{m } Matches exactly m occurrences of r
  • 14. Metacharacters ● Alternation – | Ex: /red|blue/ ● Grouping – Everything within the group is treated as a single regular expression – Backreferenced by numbers
  • 15. Metacharacters ● Repetition – r* Matches zero or more occurrences of r – r+ Matches one or more occurrences of r – r? Matches zero or one occurrence of r – r{m,n} Matches at least m and at most n occurrences of r – r{m,} Matches at least m occurrences of r – r{,n} Matches at most n occurrences of r – r{m } Matches exactly m occurrences of r
  • 16. Vim Editor - Basics
  • 17. Vim - Intro ● Vim is a text editor written by Bram Moolenaar based on 'vi' ● The original code for 'vi' was written by Bill Joy in 1976 ● Originally created for Unix systems
  • 18. Vim – Modes ● Normal Mode – characters you type are commands – Default mode ● Insert Mode – the characters are inserted as text – I, i, A, a, O, o switch to insert mode – Escp key is to come back to normal mode
  • 19. Vim – Manipulation ● Delete – d – dw, dd, D, x, X ● Copy – y – yw, yy, Y ● Join – J ● Paste – P, p ● Replace – r, R ● Undo & Redo – u, Ctrl+r
  • 20. Vim – Navigation ● h, j, k , l ● w, b, e ● 0, ^, $ – P, p ● G, gg ● % (matches {}()[ ]) ● Ctrl-b, Ctrl-f ● N G ( got to nth line)
  • 21. Vim – Search & Replace ● /pattern ● ?pattern – N, n ● :range s/oldpattern/new/options ● F, f, t, T
  • 22. Vim – Misc ● Surrounding ● Swapcase ● Vim file +pageno ● Set nu, spell ● gf ● autocomplete