Justin Mui

Justin Mui

Regular Expressions

Slide Duration:

Table of Contents

Section 1: Introduction to Ruby
Setting Up Your Environment

22m 8s

Intro
0:00
Installing Ruby
0:06
Ruby-lan.org
0:07
Three Ways of Installing Ruby
2:26
Compiling Ruby-Source Code
3:02
Third Party Tools
3:28
Other Implementations of Ruby
4:48
Windows Installation
5:21
RubyInstaller.org
5:22
Mac OSX and Linux Installation
6:13
Mac OSX and Linux Installation
6:14
Setting Up Debian/Linux
6:42
Setting Up Debian/Linux
6:43
Installing HomeBrew
6:56
HomeBrew for MAC OSX
6:57
HomeBrew Wiki
9:44
Installing HomeBrew
10:02
Setting Up Mac OSX
11:46
HomeBrew, RVM, OSX-GCC Installer, and Install Ruby 1.9.3
11:47
Ruby Version Manager (RVM)
12:11
Ruby Version Manager (RVM) Overview
12:12
Installing Ruby Version Manager (RVM): http://rvm.io
12:35
Install RVM with Ruby
14:20
Install RVM with Ruby
14:21
Install OSX-GCC-Installer
16:18
Download and Install Package for Your OSX
16:19
Install Ruby 1.9.3
17:28
Install Ruby 1.9.3
17:29
Test It Out!
18:09
rvm-help & ruby-v
18:10
Example: rvm gemset create educator
18:52
Set It As Default!
20:47
rvm Use 1.9.3@educator--default
20:48
Intro to Ruby

22m 20s

Intro
0:00
What is Ruby?
0:06
What is Ruby?
0:07
Ruby Standard Library
0:52
Who Created Ruby?
1:22
Yukihiro Matsumoto
1:23
History
2:45
The Name 'Ruby'
2:46
Ruby v0.95
3:10
Ruby v1.0
3:56
English Language Mailing List Rubytalk
4:08
ruby-forum.com & the Mailing Lists
4:27
Ruby In The West
9:51
Ruby on Rails
10:39
The Pragmatic Programmer's Guide to Ruby
11:30
rubyonrails.org
13:34
Current Ruby
14:42
Ruby 1.8.7, Ruby 1.9.3, and Ruby 2.0
14:43
Why Programmers Enjoy Ruby?
15:40
Why Programmers Enjoy Ruby?
15:41
Ruby Is An Interpreted Language
16:21
Ruby Is An Interpreted Language
16:22
What Is It Used For?
16:50
What Is It Used For?
16:51
Ruby is Object-Oriented
18:17
Example: 5.class
18:18
Example: 0.0.class
18:54
Example: true.class
19:03
Example: nil.class
19:12
Object Class
19:19
BasicObject
19:20
Example
19:52
Superclass
20:50
Fixnum → Integer → Numeric → Object
21:32
Basic Tools for Using Ruby

27m 44s

Intro
0:00
Interactive Ruby
0:08
irb: Interactive Command-Line Environment
0:09
Example
0:49
irb-v
0:50
irb-executes terminal
1:02
1.9.3-p125 > 'hi'
1:09
Live Demonstration
1:31
Why Use Interactive Ruby?
2:21
Why Use Interactive Ruby?
2:22
RDoc
3:05
RDoc
3:06
Ruby Core Documentation
3:32
Ruby Core Documentation: Example
5:30
Ruby Core Documentation: Markup
6:12
Ruby Core Documentation: Headings
7:44
Coding Example: RDoc
9:30
Why Use RDoc?
13:02
Learning Core Ruby Functions
13:03
Generating RDoc
15:31
rdoc-help # usage
15:32
Ruby Interpreter
15:57
ruby -- help
15:58
ruby [switches] [-] program [arguments]
16:16
Example: How to Run a Ruby Script
16:28
Rake
18:38
Rake Overview
18:39
Ruby Core Documentation: Rake
19:46
Coding Example: Rake
23:14
Why Was It Created?
24:30
Why Was It Created?
24:31
Lesson Summary
25:13
Lesson Summary
25:14
IDE/script Editors: MacVIM
26:24
Ruby Specifics

20m 45s

Intro
0:00
Ruby Specifics
0:06
Comments
0:51
Hashtags
1:00
Example
1:23
Multi-Line Comment
2:04
Example
3:10
RDoc Comments
4:02
When do you generate an Rdoc?
4:10
Headings and subheadings
4:24
Examples
4:48
Generating an Rdoc - example
4:50
Common Code Conventions
6:28
For every tab use two spaces indentation
7:38
Never use tabs
7:42
Common Code Conventions (Cont.)
8:18
Camel case
8:20
Snake case
9:18
Identifiers
9:44
Constants begin with CAP letter
10:00
Examples
10:10
Identifiers with Different Scoping
10:26
Global
10:34
Instance Variable
10:40
Class Variable
10:46
Examples
10:56
Reserved Keywords
12:22
Do not use reserved keywords in code
12:25
Parentheses are Sometimes Optional
13:04
Functions do not require parentheses
13:16
When in doubt, use parentheses
13:54
Examples
14:10
Newlines Are Statement Terminators
14:20
Examples
15:10
Continuation with a Period
16:20
Period means continue to next line
16:46
Multiple Statements Allowed on a Single Line
17:38
Try not to use semi-colons
17:58
Code Blocks
18:20
Use code blocks for one liners
18:28
Examples
18:40
Recommended for multiple lines
20:16
Ruby Data Types (Part 1)

29m 37s

Intro
0:00
Overview
0:10
Ruby Data Types
0:10
Numbers
0:16
Strings
0:18
Symbols
0:24
Numbers
0:30
Numeric
0:44
Float
0:50
Complex
0:56
BigDecimal
0:58
Rational
1:00
Integer (most popular) - Fixnum and Bignum
1:06
Fixnum stores 31 bits
1:18
Bignum stores larger bits
1:24
All number objects are instances of Numeric
1:28
Integer Literals
2:28
Represent whole-numbers
2:40
Examples - Different bases
2:42
Binary
3:04
Octal
3:30
Hexadecimal
3:44
Examples
4:06
Floating Point Literals
4:45
Examples
4:58
e-value can be capital or lowercase
5:30
Example
5:44
Strings
6:16
Mutable objects
6:18
Used for inserting and deleting text, searching, and replacing
6:26
String Rdoc
6:46
Definition
7:00
String Literals
8:20
Single-Quoted
8:28
Double-Quoted (most used)
8:50
Example
9:32
Escape Sequences
11:10
Newline
11:16
Tab
11:22
Double quote
11:28
Blackslash
11:36
Interpolation
11:50
Sprintf
13:48
Unicode Escaping
14:38
Example
15:50
Delimiters
16:18
Here Documents
17:18
Example
17:25
String Operators
19:58
Concatenation
20:03
Appending
20:40
String Equality
21:04
Example
21:24
Substrings
22:00
Range object (inclusive)
22:22
String Encoding
24:52
Differences between Ruby 1.8 and 1.9
24:56
Symbols
26:02
Definitions
26:04
Examples
26:46
When to use symbols
26:54
Symbols and Strings
27:42
Symbols Rdoc
28:22
Ruby Gems

25m 50s

Intro
0:00
RubyGems
0:08
What are RubyGems?
0:24
RubyGems.org
0:44
How RubyGems are used
2:06
Java's jar utility
2:50
Unix/Linux's tar utility
3:06
What is a Gem?
3:16
Definition of Gem
3:20
Version
3:34
Date
3:44
Author
3:50
Description
5:58
What Are the Uses?
4:18
Uses for Gems
4:22
Installation
5:06
How to install RubyGems
5:30
Updating to the Latest Ruby Gems
5:54
Testing
6:22
Example
6:34
Installing Rake
7:24
Example
7:46
Verifying
9:22
Example
9:56
Structure
10:56
gem.gemspec
11:30
Specification
13:40
What is in the gem?
13:42
Who made it?
13:50
Update gem version
13:58
Example
14:10
Create Our First Gem
17:20
Steps involved
17:28
RubyGems Guides
17:36
Example
20:02
Steps Review
18:56
Create Our First Gem (Cont.)
23:08
Building the gem
19:38
Example
20:00
Installing the gem
22:32
Run it
22:52
Publish it
23:04
Get Some Gems!
25:06
rake
25:14
rails
25:19
fastercsv
25:25
koala
25:37
Ruby Data Types (Part 2)

40m 24s

Intro
0:00
Ruby Data Types
0:15
Boolean
0:21
Arrays
0:27
Hashes
0:33
Range
0:37
Boolean Types
0:42
TrueClass
0:56
FalseClass
1:12
NilClass
1:18
TrueClass Examples
2:48
FalseClass Examples
3:22
Arrays
4:16
Ordered collection of objects
4:22
Can hold different objects
4:32
Starts at index 0
4:50
Array of Strings
5:50
Example
5:52
Arrays (Cont.)
6:20
Can be created using literals
6:22
Can be created using constructors
6:54
Position and indexed value
8:04
Negative Indexed Values
8:56
Shift and Unshift
10:18
Push and Pop
11:38
.delete method
12:38
Addition and Subtraction
13:32
Union and Intersection
14:48
Insert
15:52
Iteration
16:52
Arrays Rdoc
17:48
Hashes
22:08
Maps and Associative Arrays
22:44
Created using the constructor
22:56
Created using a hash literal
24:02
Stored in a hash table
25:26
Example
25:50
Accessing Key-Values
27:46
Deletion
29:48
Iteration
31:04
Hashes Rdoc
32:04
Ranges
36:40
Two dots are inclusive
36:57
Three dots are exclusive
37:16
Example
37:50
Ranges Rdoc
38:24
Objects

1h 5m 46s

Intro
0:00
Objects
0:10
Object References
1:36
Ruby Core
2:16
Example
4:30
Creating New Objects
6:00
New Method
6:08
Initialize Method
6:31
Example
7:18
Garbage Collection
9:54
Global values always reachable
10:25
Object Identity
11:08
Every object has an object identifier
11:20
Object identifier is constant and unique
11:30
Example
11:54
Object Class
12:58
Class method
13:10
Superclass method
13:28
Object Testing
14:46
is_a?
15:49
respond_to?
16:26
String and Regexp
18:10
Comparing two object instances
20:06
Example
23:30
Object Equality
25:48
Comparing objects
25:54
equal?
25:58
Popular way to test for equality
27:16
Opposite way to test for equality
27:25
Arrays
28:30
Hash
29:42
Case equality operator
30:47
Class tests
31:16
Range tests
31:48
Symbol tests
32:32
Object Conversion
33:14
Explicit conversion
33:54
Implicit conversion
35:00
Example
36:12
Object Conversion: Kernel Module
38:22
Array
38:38
Float
39:26
Integer
39:58
String
40:10
Example
40:34
Object Conversion: Coerce
42:00
Used for mixed type numeric operations
42:08
Example
43:40
Object Conversion: Boolean
47:42
Every object has a boolean value
47:44
Example
48:54
Object Copying
50:52
dup
50:58
clone
51:03
Example
51:42
Object Freezing
57:36
Object Marshaling
58:38
Save state
59:04
Load state
59:27
Example
59:32
Tainted Objects
1:01:50
taint
1:02:08
farm field
1:02:12
Untrusted Objects
1:04:06
trust
1:04:24
untrust
1:04:34
untrusted?
1:04:42
Loops

38m 54s

Intro
0:00
Loops
0:12
while and until
0:48
for and in
0:54
iterators
1:04
enumerable in objects
1:06
While-loop
1:14
Will keep going is condition is true
1:18
Until-loop
2:58
Will keep going until condition becomes true
3:06
Single Expression Loops
4:20
Compact form
4:30
Expressed as a modifier
4:42
Do-While Loop
5:52
Executes body first
6:06
Do-Until Loop
7:54
Similar to do-while loop
7:58
Using Break Inside Loops
8:54
break
8:58
For-In Loop
11:56
for-loop
12:06
var
12:34
collection
12:54
body
13:00
Examples
13:08
Examples (Cont.)
15:54
Nested loops
16:40
Numeric Iterators
18:32
upto
18:40
downto
18:42
times
18:48
Examples
20:28
External Iterators
21:00
Enumerator class
21:04
Rdoc
21:16
Enumerables in Objects
24:35
Enumerable is a mix-in
24:41
RDoc
25:24
Commonly Used Enumerables in Objects
27:01
Array
27:19
Hash
27:51
Range
28:47
Examples
29:29
Enumerables in Objects (Cont.)
31:13
File Processing
31:15
Example
31:45
Enumerables in Objects (Cont.)
33:07
collect
33:23
select
34:11
reject
34:59
inject
35:29
Strings

28m 30s

Intro
0:00
Strings
0:08
Why do you want to get familiar with strings?
1:00
String Creation
1:16
new
1:28
empty?
1:50
length or size
2:10
Example
3:12
String Manipulation
4:40
slice
4:56
square brackets [ ]
5:02
token
5:40
[fixnum]
6:52
offset and length
8:40
chaining
12:42
String Insertion
12:56
insert
12:58
positive or negative index
13:46
String Updates
15:24
[token]
15:36
Examples
16:40
chop or chop!
17:54
chomp!
18:56
gsub
20:28
String Deletion
21:38
delete
21:38
String Reversal
22:46
reverse
22:52
String Manipulation
23:16
split(pattern=$, limit)
23:22
pattern
24:10
limit
24:15
upcase or upcase!
25:28
downcase or downcase!
26:02
swapcase
26:24
Incrementing Strings
27:26
next or next!
27:32
Check Out the Other Lessons
28:00
Ruby Data Types Part 1
28:12
Regular Expressions
28:18
Regular Expressions

33m 27s

Intro
0:00
Regular Expressions
0:10
How to create a regular expression
0:48
What goes inside
1:36
Metacharacters
3:10
Bracket expressions
3:14
Quantifiers
3:18
Anchors
3:20
Metacharacters
3:30
word and non-word characters
4:04
digit and non-digit characters
4:44
hexdigit and non-hexdigit characters
4:56
whitespace and non-whitespace characters
5:08
Examples
5:24
POSIX Bracket Expressions
7:48
Non-POSIX Bracket Expressions
9:48
Bracket Expression Examples
10:58
Quantifiers
12:34
Examples
13:30
Character Properties
17:24
Similar to POSIX bracket classes
18:22
More Character Properties
18:48
Examples
19:32
Anchors
20:08
Examples
21:14
Regular Expression Matching: Regexp Object
22:40
match
22:51
Regular Expression Matching: String Object
24:14
match
24:26
Regular Expression Modifier Characters
25:14
pat
25:38
Example
26:42
Regular Expression Modifier Objects
27:14
Example
28:38
Regexp Rdoc
30:40
Arrays

14m 35s

Intro
0:00
Arrays
0:12
Creating an Array with a Block
0:50
Alternative Ways to Create an Array
3:52
Checking the Class
5:14
Iterate through the array
5:26
Call the class method
5:28
Array Shortcuts
6:38
at(index)
6:44
delete_at(index)
7:28
first(n)
8:28
last(n)
9:28
Removing Duplicates
9:58
uniq or uniq!
10:04
Sorting the Array
10:48
sort or sort!
10:54
Getting the Index
11:35
index
11:56
rindex
12:38
Multidimensional Arrays
12:56
flatten
13:33
Check Out the Earlier Lesson
14:16
Ruby Data Types Part 2
14:26
Hashes

27m 48s

Intro
0:00
Hashes
0:12
Creating Hashes
1:18
Setting a Default Value
2:24
Accessing Hashes
4:16
Accessible by keys or by values
4:28
Keys must be unique
4:36
Creating Hashes
5:16
Comma-separated list
5:42
Hash rocket
8:28
Examples
10:16
Iterating Keys and Values
11:43
each_key
12:04
each_value
14:04
Merging Hashes
16:10
merge(other_hash)
16:20
Sorting Hashes
18:46
Replacing Hashes
20:57
replace(other_hash)
21:18
Converting Hashes to Other Classes
22:04
to_a
22:22
to_s
23:22
Example
24:34
Check Out the Earlier Lesson
27:22
Ruby Data Types Part 2
27:32
Math Operations, Part 1

28m 47s

Intro
0:00
Math Objects
0:12
Numeric
0:26
Integer
0:38
Float
1:02
Fixnum
1:14
Bignum
1:56
Rational
2:04
Math
2:24
Math Operations
2:36
Example
3:14
div(numeric)
4:54
divmod(numeric)
6:30
modulo(numeric)
7:23
quo(numeric)
8:18
remainder(numeric)
9:35
Operation Precedence 1 of 3
10:35
Operation Precedence 2 of 3
13:18
Operation Precedence 3 of 3
14:28
Abbreviated Math Operations
14:54
Move the operator in front of the equal sign
15:52
Numbers
16:36
Numeric Class
17:06
Numeric Methods
18:41
ceil
18:52
floor
19:32
round
19:50
Example with Numbers
20:20
Numeric Methods (Cont.)
22:20
truncate
22:28
num.step(limit, step)
23:02
Numeric Rdoc
25:26
Math Operations, Part 2

28m 51s

Intro
0:00
Math Operations
0:12
Math Module
0:24
Rational Numbers
0:44
Complex Numbers
0:52
Prime Numbers
0:58
Matrices
1:06
Math Module
1:12
PI and E
1:32
Math Module Methods
2:47
atan2(x,y)
2:56
cos(x)
3:14
exp(x)
3:44
Examples
4:38
log(x)
5:44
log(num, base)
6:34
log10(x)
7:04
sin(x)
7:34
sqrt(x)
7:52
tan(x)
8:06
Math Functions: Part 1 of 3
8:12
Math Functions: Part 2 of 3
9:32
Math Functions: Part 3 of 3
10:19
Math Module Rdoc
11:25
Rational Numbers
13:23
How to use
14:06
Example
15:02
Mathematical Ruby Scripts (Mathn)
16:25
Example
17:28
Complex Numbers
18:26
polar
18:56
rect
19:10
Complex Number Examples
19:18
Prime Numbers
20:14
each(ubound=nil)
20:44
prime?
21:22
Example
21:58
Matrices
23:15
build(row_size, column_size=row_size)
23:44
Example
24:44
Matrix Rdoc
24:58
Dates and Times

26m 1s

Intro
0:00
Dates and Times
0:12
Time Class
0:38
Methods of the Time Class
1:43
now
1:44
at(time)
2:10
Epoch & Unix Timestamp Conversion Tools
3:19
Components of a Time
5:07
Convert Time to an Array
5:54
to_a
6:08
Creating a New Time
6:48
Time.local
7:08
Year is required
7:22
Time.utc
8:12
What should be specified
9:30
More Methods of the Time Class
10:16
strftime(string)
11:26
RDoc
12:50
Date Library
16:46
Initializing a New Date
17:08
Parsing Dates
18:28
parse(string)
18:42
Today's Date
19:19
Date.today
19:22
Tomorrow's Date
20:22
Next
20:28
Next week
21:22
Count Down
21:26
Count Up
22:37
Components of a Date
23:20
Converting to Datetime
23:48
to_datetime
24:00
Initializing a Datetime
24:24
Converting to Time
25:23
self.to_time
25:32
Methods: Part 1

31m 24s

Intro
0:00
What is a Method?
0:12
Basic Method
0:58
Return Value
4:37
return
4:46
Factorial Example
6:18
Example
8:46
Return Two Values
10:06
Set the return keyword
10:14
Collected and returned as an array
10:28
Undefining Methods
11:22
undef method_to_undefine
11:44
Example
12:32
Method Names
13:02
Begin with lowercase letter
13:16
Separate longer words with underscores
13:26
Can end with equal sign, question mark, or exclamation point
14:03
Equal sign
14:26
Method Names with Question Mark
14:44
empty?
15:24
Method Names with Exclamation Point
16:01
mutators
16:12
! means use with caution
16:46
Method Aliases
18:05
alias new_method existing_method
18:42
Operator Methods
20:00
Operators
20:02
Array Operators
20:10
Unary Operators
20:32
Binary Operators
20:40
Example
21:28
Methods and Parentheses
25:00
Optional in most cases
25:20
Required in other cases
27:13
Methods and Blocks
27:54
Associated with blocks
28:18
block_given?
28:26
yield
28:36
Example
29:24
Methods: Part 2

20m 11s

Intro
0:00
Methods with the Unary Ampersand Operator
0:14
&
0:34
Block to a Proc
0:56
Example
2:02
Proc object
3:58
Example
5:04
Methods with Default Values
5:54
Example
7:12
Methods with variable-Length Arguments
8:05
How to create it
8:36
Example
11:06
Using Hashes with Arguments
13:02
Multiple arguments
13:08
Solution
13:30
Example
14:56
Rdoc
18:12
Classes: Part I

26m 51s

Intro
0:00
Classes
0:10
Definition of a class
0:14
Class represents a container
0:32
Can be reused
0:46
Creating our First Class
1:00
Keyword class will create new class
1:06
Name must begin with capital letter
1:30
Instantiating Our First Class
2:46
New will create a new instance of class
2:58
Initializing Values
3:58
Definition of def
4:14
Instance method
5:08
Example
7:02
Defining the to_s Method
8:24
Creating a string representation class
8:34
Example
10:54
Self in the Class
12:16
Definition of self
12:26
Example
13:54
Accessor Methods
15:52
getter methods
16:22
Example
17:00
Setter Methods
18:00
Mutator methods
18:02
Example
19:46
Automating Getter and Setter Methods
21:10
Defined in the module class
21:30
attr_reader
21:54
attr_writer
22:48
attr creates getter and setter methods
23:50
Example
24:28
Notes on Ruby's Accessor Methods
25:32
Classes: Part II

26m 42s

Intro
0:00
Defining Operators
0:10
You can define arithmetic operators
0:32
Unary Operators
0:46
Let's define operators in our class!
0:56
Example
2:52
Class Methods
6:24
Examples
6:56
Opening Up the Class
9:38
Adds an additional method
9:54
Examples
11:04
Array and Hash Access Method
15:40
Use square brackets
16:02
Define your own has access method
16:08
Example
16:56
Enumerating The Values
18:40
Define the each iterator
18:40
Testing for Equality
19:36
Class Triplex
19:50
Examples
20:54
Constants
25:00
Usually defined at the top of class
25:24
Classes: Part III

53m 36s

Intro
0:00
Class Variables
0:14
Example
2:16
Ruby Glass Jar Example
8:50
Class Instance Variables
10:20
Instance variables of class objects
10:46
Advantage of class instance variables
11:18
Examples
11:30
Method Visibility
16:16
Three types of method visibility
16:26
Public methods
17:34
Private methods
17:38
Protected methods
18:04
Invoking Method Visibility
19:21
Public , Protected, and Private Visibility
19:22
Invoking Method Visibility With Arguments
21:39
Example: Invoking Method Visibility
22:12
Class Visibility
23:31
Instance and Class Variables are Private
23:32
Constants are Public
24:00
Makes Existing Class Methods Private
24:27
Makes Existing Class Methods Public
25:08
Example: Class Visibility and class GlassJar
25:43
Subclassing
27:08
Subclassing: Subclass and Superclass
27:09
Example: Subclassing
29:43
Inheritance
30:05
Inheritance
30:06
Example: Inheritance
31:25
Subclassing and Inheritance
31:34
Descendants
31:41
Ancestors
31:56
More On Descendants and Ancestors
32:08
Extending a Class
33:27
Extending a Class
33:28
Coding Example: Extending a Class
34:24
Overriding a Method
36:41
Overriding a Method
36:42
Coding Example: Overriding a Method
37:18
Modifying Methods with Chaining
38:52
Modifying Methods with Chaining
38:53
Super
39:25
Coding Example: Modifying Methods with Chaining
39:51
The Singleton Pattern
44:52
Introduction to The Singleton Pattern
44:53
Setting Up Singleton
45:28
The Instance Method
45:58
Rdoc for Singleton: Usage
46:23
Rdoc for Singleton: Implementation
47:45
Coding Example: Singleton
49:38
Modules

24m 19s

Intro
0:00
Modules
0:04
What is Modules?
0:05
Modules Examples
0:40
Modules: Mix-Ins
3:31
What is a Mix-in?
3:32
Modules: Namespace
4:07
What is a Namespace?
4:08
Why Use a Namespace?
5:13
Example of a Namespace Module
5:59
Example of Mixing in The Module Into the Global Scope
6:00
Modules: Creation
7:04
How to Create a New Module?
7:05
Modules: Usage
8:19
How to Use It?
8:20
class Poker & class Bridge
9:13
Creating Our Module as a Mix-In
9:41
Example of a Module Using Instance Methods
9:42
Coding Example
10:20
Creating Our Module as a Namespace
12:11
Implement Class Methods for the Module
12:12
Coding Example
14:56
Loading Our Module
19:46
Loading Our Module Overview
19:47
Require & Load
20:15
Coding Example: Loading Module
20:48
Lesson Summary
23:36
Loading...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
Bookmark & Share Embed

Share this knowledge with your friends!

Copy & Paste this embed code into your website’s HTML

Please ensure that your website editor is in text mode when you paste the code.
(In Wordpress, the mode button is on the top right corner.)
  ×
  • - Allow users to view the embedded video in full-size.
Since this lesson is not free, only the preview will appear on your website.
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Regular Expressions

  • Regular Expressions are used to match a pattern against strings
  • RDoc: http://www.ruby-doc.org/core-1.9.3/Regexp.html
  • To create a regular expression, you need to look at metacharacters, bracket expressions, quantifiers, and anchors
  • Metacharacters can be used to match a variety of expressions like word characters, digits, hexdigits, and even whitespace
  • Quantifiers allow you to match a character a certain amount of times and even set bounds to the minimum amount to maximum amount
  • Anchors are metacharacters that match the zero-width positions between characters
  • Anchors are used to match to a specific position
  • Regular expression modifier characters allow you to control how a pattern can match

Regular Expressions

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
    • Regular Expressions
    • How to create a regular expression
    • What goes inside
    • Metacharacters
    • Bracket expressions
    • Quantifiers
    • Anchors
  • Metacharacters 3:30
    • word and non-word characters
    • digit and non-digit characters
    • hexdigit and non-hexdigit characters
    • whitespace and non-whitespace characters
    • Examples
  • POSIX Bracket Expressions 7:48
  • Non-POSIX Bracket Expressions 9:48
  • Bracket Expression Examples 10:58
  • Quantifiers 12:34
    • Examples
  • Character Properties 17:24
    • Similar to POSIX bracket classes
    • More Character Properties
    • Examples
  • Anchors 20:08
    • Examples
  • Regular Expression Matching: Regexp Object 22:40
    • match
  • Regular Expression Matching: String Object 24:14
    • match
  • Regular Expression Modifier Characters 25:14
    • pat
    • Example
  • Regular Expression Modifier Objects 27:14
    • Example
    • Regexp Rdoc

Transcription: Regular Expressions

Welcome back to Educator.com.0000

Today's lesson is on regular expressions.0002

What are regular expressions? They are used to match patterns against strings.0008

In Ruby, you will see the object that is called RegExp, and it is the main object to create regular expressions.0016

It is used to match a pattern against strings, as I just said.0036

How do you create a regular expression?0044

There are three ways: one is using the forward slashes--this is the most popular way, and it follows a similar syntax as in other languages, so if you are coming from another language, you will probably see this the most often; it's the most familiar to you.0046

Another way is the literal syntax.0067

This one is more well-known in Ruby; it uses the percentage, r, and the curly braces.0070

You have it through the constructor; the Regexp.new creates it through the constructor.0083

You saw on that slide, it has the dot, dot, dot; what goes in there...you can put a different type of things in there.0097

For example, for this one, I say it's two words, and for that I have this regular expression--what is says here is, "I'm going to take a word character--one or more word characters, a space, and then one or more word characters again."0112

That is what that regular expression is matching.0135

Another one I have here is a range of digits--so it takes one or more digit characters, which is 0 through 9.0140

The other one I have here is just dot, dot, dot...and this one is for you to be creative.0150

If you were just to put ... , it would match three wildcard characters.0160

So, this is just...be creative, and update that to make your own regular expression; that is what I want you to do.0166

I'm going to write that down: Make your own.0176

To create regular expressions, we need to look at four different key elements.0185

One is metacharacters, bracket expressions, quantifiers, and anchors.0192

First, we are going to look at metacharacters.0207

For this one, we are going to look at this dot; this will match any character except a new line.0211

The next one we are looking at is this multiline mode.0224

It just adds this m at the end, so it can match new lines.0228

Here, with the slash and the lowercase w...this matches a word character.0236

A word character is a lowercase a through z, capital A through Z, 0 through 9, or this underscore character.0245

Another one we can do is, with a capital W, we can match a non-word character.0256

This is not a through z, not A through Z capital, 0 through 9, or the underscore.0264

The slash d matches a digit character; it's 0 through 9.0280

A capital D does a non-digit character.0288

You can match hex digit characters, 0 through 9, a through f...0293

Capital H--a non-hex digit character...0301

The small s matches a white space character--you have your new lines, your tabs...0308

The capital S matches a non-white-space character--so not those.0316

Here, we have a few examples.0325

For the example on top, I have this sentence here; what this regular expression is going to do is, I'm calling a literal, and it's going to get two words.0331

It says, "I'm going to match the first two words I see."0346

So, it matched this first part of the sentence.0350

On this next section here, I have another string, and this one is going to match a range of one or more digits, a space, and then a word, which is one or more word characters.0357

Of course, it's matching the beginning again, so it's the "1000 bottles".0381

Notice, for this one, we are using the token method by string, and we are passing a regular expression, versus passing the actual quoted value for the string.0387

Here is another way you can do regular expressions.0405

This is defining the regular expression through a constructor method; what it's going to do is, it's going to create that object, and you can pass that in.0406

If I do Regexp.new, it's going to match a word, a space, another word, a space, and another word.0423

I'm just going to call this regular expression 'Three words.'0433

I'm telling the string, "Match me a pattern that has three words," and it finds, "1000 bottles of" and matches that.0436

We can also do this same thing using the regular expression literal.0451

You will notice that it has the same value, also.0458

The next thing we are looking at is bracket expressions.0466

First, we have the POSIX bracket expressions; this one gives you a more refined, specific case of things you can match against.0474

The first one, alnum, is alphabetic and numeric character.0487

Notice, to do this match, it has two square brackets.0492

The second one after that is alphabetic characters; it uses this alpha.0497

The third one is this blank, and that matches a space or tab.0503

This one here, cntrl, matches the control character.0515

For the fifth, we're matching a digit: remember, that is our 0 through 9--so it is matching our digit character.0520

Our sixth one matches a non-blank character--this graph--it excludes spaces, control characters, and similar...0531

The next one matches lowercase alphabetical characters--it uses lower.0543

As you can see, this is just using tags, but it is matching these different characters using them.0550

It's not scoped as deeply as the metacharacters were, but you can refine...look at these different patterns and match more specifically to it.0557

The next one is print: it's like graph, but it includes a space character.0575

The last one can match punctuation characters.0581

Also, there are more bracket expressions than this, but I'm just showing you some of the more popular ones.0590

Here is the space--it's very easy to understand--it's a white space character.0596

And then, this upper matches the uppercase alphabetical characters, and xdigit is a digit allowed in hexadecimal--a hexadecimal number--0 through 9, a through f, and capital A through F.0604

Then, there are also non-POSIX bracket expressions, which are allowed in Ruby, too.0623

If I pass in word, it's going to match "a character in one of the following Unicode...categories: Letter, Mark, Number, Connector_Punctuation."0629

Then, if you pass ascii, it's a character in the ASCII character set, so that one is probably more of a popular one you would be using.0644

Let's look at some examples of bracket expressions.0654

We can do it without bracket expressions, but this allows us the flexibility to do both; Ruby allows us to have this open scope to match using the ways we find most popular.0659

Here, we have our string: "A fool thinks himself to be wise, but a wise man knows himself to be a fool."0676

Here, we are matching three words using the bracket expression.0683

One word, one word, one word...also, I know I haven't told you yet, but this plus is a quantifier.0687

We will go over that...but that is the thing that says "one or more"--so it says "one or more word characters."0697

Notice, for this next example, I take that regular expression, and I save it to a variable, so I can pass it in here; and I get the same value.0712

And then this last example: what I'm telling it is to match a digit from the string, but since there are no digits in it, it's going to just return nil.0731

If there is any character that doesn't exist, it will return nil, just like that.0745

Now, looking at the quantifiers: you have already seen this one with the plus that says "one or more times."0753

Another popular one you will use is the star; it says "zero or more times."0760

Question mark says "zero or one time."0766

Then, we have these four bracketed lines: the curly n--exactly n amount of times: so if I pass a 10 there, exactly 10 times--that is how it is going to match.0772

If I do n, comma, it's going to match--for 10, it would be 10 or more times.0785

If I pass a comma, and then pass a number in there, I'm going to match m or less times.0792

I can pass both arguments in it, and it's going to say "at least n and at most m times" for the quantifiers.0799

Let's go through some examples.0809

Here, I've made this regular expression--it's called the namefinder: what it does is, it looks for two words, one word with the uppercase and then the lowercase characters after that; then it's going to look for a space; and then it's going to look for another uppercase character and a range of lowercase characters from there.0812

So, I pass this namefinder to the string: you will notice it finds that pattern right there.0839

For our next example, I am making a regular expression; it will say...it's going to look for the letter a.0849

Before that, it could be a word character of 0 or more, the same as afterwards.0860

I pass that in here, in this pattern, and it finds the first word that contains the letter a, which is name.0868

This regular expression says, "OK, a could be at the beginning of this word; it could also be in the middle; of course, it could also be at the end."0882

Either way...even if it's by itself, it will match that as a word, too...so if it's just a single letter a, this expression will also match that.0896

So, one of these scenarios, if they pop up, it's going to return that.0909

The first one it sees is the a here--it's the third word--so it returns that as the matching pattern, and it returns that as what it gets from this regular expression.0915

The next one we are looking at is those curly brackets.0934

We have our string; it has three a's, four b's, five c's.0942

For this one, I have this a with the curly brackets; it's one, comma, four.0949

It matches with the three letters there.0957

This says that a is shown at least one time and at most four times.0965

That is what this one and the four is.1000

Here, we have the same for b, but the b is shown at least one time and at most two times, and here you go--it returns two, which is the most.1004

And then, we have this one with the five, comma, which is "if c is shown five or more times," and it returns ccc.1026

The next thing we're looking at is the character properties.1043

This is another way to do a matching with the regular expression.1050

It uses the slash, p, and the curly brackets.1057

You will notice it has a lot of the similar classes as POSIX.1063

You still have your alnum for alphabetical/numeric; you have alpha for alphabetical characters, blank for spaces or tabs, cntrl for a control character, digit, and graph.1069

For graph, that is your non-blank character again.1088

Notice--it's interesting how I'm still passing my regular expression pattern, but I am using this new p with curly brackets, and I'm passing the actual thing I want to find into this pattern.1093

It's...p...you enter a pattern in here...1115

Here are some more examples.1126

You can pass in lower for lowercase alphabetical characters, punct for punctuation characters, space for a white space character, and upper for uppercase alphabetical.1136

This p--you can also match a character's Unicode script.1152

If you look at the RDoc, there are other character properties in there.1162

We will look at that, too.1167

Here is an example with digit.1170

I'm using my quantifier "one or more."1173

I have my digit regular expression match.1177

It's going to take the first digit--one or more--it's going to match the whole thing, so it sees 5000 and that is what it returns here.1182

The next one we are looking at is alphanumeric characters, and then it's going to look for a space and another alphanumeric character.1191

The first one that comes up is this "apples in," so that is what it matches, and it returns that--that is the token it finds.1199

The next thing we're looking at is anchors.1209

This will be pretty important, too, because you want to anchor your regular expression--not always at the beginning: you might want to do the end.1214

These are metacharacters that match the zero-width position between characters.1224

It is used to anchor the match to a specific position.1230

Here we have our ^; it matches the beginning of a line; a $ matches the end of a line.1235

The A matches the beginning of a string; the Z matches the end of string; if the string ends with a new line, it matches just before the new line.1245

Lowercase z matches the end of string.1260

There are a lot of other anchors in the RDoc, but these are the popular ones.1263

Let's look at some examples of anchors.1272

For this one, I'm using the dollar sign that will match at the end of the string.1276

Instead of starting at the beginning, it's going to match from the end, look for patterns from that, and if it finds it, it will return me that substring that is part of it.1281

Here, what I want to do with this string is, I want to get the last sentence.1293

There are two sentences here; I want to get this last one.1298

In my regular expression, what I'm looking for is, first, it's going to anchor that to the end to do a match.1302

It's going to be looking for a period or a question mark, and then it's going to look for a group of words.1310

That will match my last sentence.1322

When I pass that in into my string, it does get, "What is your name?"1324

The problem is, it also matched this space.1332

What I'll do is, I'll just chain another method here; I call this method called strip--this is one of the string methods--and that is going to remove that blank space that was in front.1337

It's going to get rid of that, and then I get this string here.1351

The next thing we will look at is regular expression matching with the Regexp object.1361

For this, we are looking at the match method.1368

This is part of the Regexp object, and it allows matches with strings.1373

For this one, I have the string, "Hello, my name is Mr. Smith. What is your name?"1380

This object is a regular expression.1388

I call the match method that is part of the regular expression, and I pass in the string, and what it will do is, it's going to look for Mr. in this string.1394

It's going to find it here, and then it will return...it says, "Hey, I found the match data, and I found your token."1419

Now, if I pass in there a pattern--like I pass in the w+, it's going to match the "Hello" at the beginning.1428

Again, if I also pass in...if I start with a regular expression and do a match with that string, and that token doesn't exist, it's going to just say nil.1442

The next regular expression matching we're going to look at is the String object; this does the exact same thing, but we're starting with the String object.1455

We have this match method that is part of the String object; it allows regular expression matches and the reverse of the regular expression .match method.1463

Here I have a string: "Hello, my name is Mr. Smith. What is your name?"1477

At the beginning, we start with a string, and then we call this match method.1481

It does the exact same thing: it returns the match data.1488

It's flexible; we could start with the regular expression or the string, depending on how your code is going.1493

I have the string.match, and we pass a regular expression here, and it says nil.1502

The next thing we're going to look at is regular expression modifier characters.1514

This is located at the end of a delimiter, and it has one or more single-letter options.1520

This will control how the pattern can match.1527

For example, let's say pat is my regular expression pattern; this is whatever you put in those parentheses; this could be...let's try a digit character, space, another digit character.1532

That is our regular expression.1554

I'm just saying pat is just some pattern.1557

What is important here is these modifiers.1562

This i says you can ignore case--it doesn't matter if it's uppercase or lowercase; it will match it because I put that modifier there.1566

If I put an m, it makes the dot match newline, so if you put a period in a regular expression, it will match newlines.1578

If I put an x, it says to ignore the comments and white space in the pattern.1588

If I put a 0, it says you can perform interpolation--only once, though.1592

Let's take a look at this...we have our example here again, but notice with my match I used a lowercase n.1598

Notice, it still matches it: the reason it does is because of this i here, which says to ignore that case.1607

Again, for that same setup, if I go using the regular expression object, and I have that modifier i, it will get the same result with that match data.1616

The last thing I want to talk about is the regular expression modifier objects.1635

We have Regexp::IGNORECASE, Regexp::EXTENDED, and Regexp::MULTILINE.1640

You can pass these in as options in your constructor.1646

We have our Regexp.new; you have your pattern here; and then you can pass the options as a second argument--and this is optional.1651

For example, I use that same sentence--we've been really pummeling this sentence with regular expression matches--and I say IGNORECASE.1677

It's another way to do the modifier in the constructor; this is the way you would do it.1690

And then, that will match that string, even though this m is lowercase.1694

Let me show you, in the terminal, how you can do this.1702

You can pass an array as an argument, so let's go ahead and use the example again: for options, I'm going to just pass an array...Regexp...IGNORECASE...1716

These are constants that are part of the Regexp object...Regexp::MULITLINE...notice, I've passed in all three options.1732

Also, notice that these values--these constants--are just flags as part of that object.1744

Then, let's create my string that I want to work with.1754

"Hello, my name is Mr. Smith. What is your name?"1759

Then, I'm just going to create my regular expression, and then pass in options...let's do a match string.1766

I'm creating a constructor--it's calling new--and the token I'm looking for is the Mr.1782

I can also choose it as a regular expression pattern.1796

These options...that array, I'm just going to match it with that string.1800

It actually didn't find it...let's try putting back those quotes...and it does find it there.1808

That is the reason--because we're using a constructor here; we are not using the literal sense.1818

That is how to pass those arguments in.1824

Let me show you the RDoc for regular expressions.1831

We have gone through a lot of these examples already.1838

Here is the one with the forward slash and the Ruby regular expression literal.1843

Notice--we talked about this before--it uses the equal tilde sign, and you can pass a regular expression with the equal tilde and what you are looking up.1852

It will tell you the position it starts with; this one starts with 0.1863

You can also use the .match that we talked about.1867

We already went through some of this: metacharacters, we talked about...1877

Escapes--we didn't actually go over that, but you can escape the special characters, like the plus and the question mark.1882

If you're matching those, make sure to escape those out.1891

Also, notice that you can use a Unicode; here is an example of some Unicode.1895

That gets passed as some international characters.1900

We talked about how the character class uses those square brackets.1911

Over here, notice "aeiou"--if any of the second characters matches these ones, the second letter matches these ones...word matches, ward matches...it will create a match with that regular expression, too.1916

With the same character class, with 0-9, a through f, if it's one of those that falls in that range, it will match it.1937

Here, 9 does, and 9f.1945

We already saw the carat--this one says it's not a through e, not g through z...and it will match f, and it matches f there, because it doesn't fall in that character set.1949

We already went through these metacharacters.1965

You will see that there is actually quite a bit more here.1968

We went through the repetition, and there is also grouping that is allowed--so you notice these parentheses.1974

I will let you look at this RDoc so you can get more details on these regular expressions.1991

Otherwise, this is the end of the regular expression lesson, and thank you for watching Educator.com!1999

Educator®

Please sign in to participate in this lecture discussion.

Resetting Your Password?
OR

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Membership Overview

  • Available 24/7. Unlimited Access to Our Entire Library.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lecture slides for taking notes.
  • Track your course viewing progress.
  • Accessible anytime, anywhere with our Android and iOS apps.