Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Comprehensive Ruby Programming

You're reading from   Comprehensive Ruby Programming From beginner to confident programmer

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781787280649
Length 330 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jordan Hudgens Jordan Hudgens
Author Profile Icon Jordan Hudgens
Jordan Hudgens
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Introduction to the Ruby Programming Language FREE CHAPTER 2. Ruby Variables 3. Ruby Strings 4. Working with Numbers in Ruby 5. Ruby Methods 6. Ruby Iterators and Loops 7. Ruby Collections 8. Ruby Conditionals 9. Object-Oriented Programming in Ruby 10. Working with the Filesystem in Ruby 11. Error Handling in Ruby 12. Regular Expressions in Ruby 13. Searching with grep in Ruby 14. Ruby Gems 15. Ruby Metaprogramming 16. Ruby Web Frameworks 17. Working with APIs in Ruby 18. Ruby Algorithms 19. Machine Learning

Regular expression code example

Let's start with some basic regular expression matchers. We'll begin with storing a string sentence in a variable:

string = "The quick 12 brown foxes jumped over 10 lazy dogs" 

First off, let's check whether the preceding string contains the letter o, and to do that, write the following code:

p string =~ /o/ 
In Ruby, anything contained within // is a regular expression.

Now if you execute this code, the output should be 15. The value printed out would be 15 because the matcher would find the letter o at position 15.

If you want to find an entire word, you can do that with this code:

p string =~ /quick/ 

The output of this will be 4 because the word quick begins at position 4.

A more practical example would be to check whether a letter, say z, is present in a string. If it's present, then it's a valid string; otherwise...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image