Computer Science‎ > ‎

Introduction to Ruby - Regular Expressions, Match, Scan



Programming With Ruby

"My e-mail address is [email protected]. And you can check out my website at http://www.xyzabc.co.in/"

Let's try out a few simple regular expression based techniques to detect 1) The E-mail address  2) Website  3) Domain for the email address.


# This is a text fragment from which we're going to look for stuff using regexes.

text = "My e-mail address is [email protected]. I have another one too which I check less frequently, [email protected]. And you canheck out my website at http://www.xyzabc.co.in/ "

# Note that this is a very simple version of a regex for e-mails and might not be handle many complicated cases and different domain extensions

emailRegex = /[^\b\s\t\n]*\@[a-z0-9\-\_]*\.[a-z]{2,4})/i
=> /[^\b]*\@[a-z0-9\-\_]*\.[com|org|edu|co\.in]/i

# Scan returns an array of strings which match the given regex.

irb(main):101:0> text.scan(emailRegex)

# Match will return only the first string which matches the given regex.

irb(main):102:0> text.match(emailRegex)
=> #<MatchData "[email protected]">

#To directly access the string returned by match do this ..

irb(main):103:0> text.match(emailRegex).to_s








Check out some of our other Ruby Tutorials :

Introduction to Ruby

 Introduction to Ruby and some playing around with the Interactive Ruby Shell (irb) Introduction to Ruby - Conditional statements and Modifiers: If-then, Unless, Case Introduction to Ruby Comments - Single and Multi-Line comments Introduction to Ruby Loops - Using While, Until, For, Break, Next , Redo, Retry
 Introduction to Ruby - Arrays - Sorting, Filtering (Select), Transforming, Multi-Dimensional Arrays Introduction to Ruby - Strings Introduction to Ruby - Making a Script Executable Introduction to Ruby - Regular Expressions, Match, Scan
 Introduction to Ruby - Computing Factorials Recursively : An Example of Recursion Introduction to Ruby - Binomial Coefficients (nCr) : An Example of Recursion Introduction to Ruby - Computing a Power Set : An Example of Recursion Introduction to Ruby - Towers of Hanoi : An Example of Recursion
 Introduction to Ruby - Strings: Substitution, Encoding, Built-In Methods 



Basic Data Structures With Ruby




Programming With Ruby

 Introduction to Ruby and some playing around with the Interactive Ruby Shell (irb)

Introduction to Ruby - Conditional statements and Modifiers: If-then, Unless, Case

Introduction to Ruby Comments - Single and Multi-Line comments

Introduction to Ruby Loops - Using While, Until, For, Break, Next , Redo, Retry

Introduction to Ruby - Arrays - Sorting, Filtering (Select), Transforming, Multi-Dimensional Arrays 

Introduction to Ruby - Strings

Introduction to Ruby - Making a Script Executable

Introduction to Ruby - Regular Expressions, Match, Scan

Introduction to Ruby - Computing Factorials Recursively : An Example of Recursion

Introduction to Ruby - Binomial Coefficients (nCr) : An Example of Recursion

Introduction to Ruby - Computing a Power Set : An Example of Recursion

Introduction to Ruby - Towers of Hanoi : An Example of Recursion

 Introduction to Ruby - Strings: Substitution, Encoding, Built-In Methods

Basic Data Structures in Ruby - Insertion Sort

Basic Data Structures in Ruby - Selection Sort

Basic Data Structures in Ruby - Merge Sort

Basic Data Structures in Ruby - Quick Sort

Functional Programming with Ruby

Basic Data Structures in Ruby - Stack

Basic Data Structures in Ruby - The Queue

Basic Data Structures in Ruby - Linked List - ( A Simple, Singly Linked List)

Basic Data Structures in Ruby - Binary Search Tree