Slide 1

Slide 1 text

Regular Expressions are your friend

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Hello

Slide 5

Slide 5 text

Aaron Kalin @martinisoft

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

We’re hiring!

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Warning (memes ahead!)

Slide 12

Slide 12 text

Interaction!

Slide 13

Slide 13 text

Regular Expressions

Slide 14

Slide 14 text

Regex

Slide 15

Slide 15 text

Pattern

Slide 16

Slide 16 text

/Pattern/

Slide 17

Slide 17 text

Subject

Slide 18

Slide 18 text

Match

Slide 19

Slide 19 text

Editors vim emacs TextMate RubyMine Redcar BBEdit ...

Slide 20

Slide 20 text

Languages perl ruby python lua java javascript ...

Slide 21

Slide 21 text

Meta language

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Menu

Slide 25

Slide 25 text

history basics good practices ruby tips

Slide 26

Slide 26 text

History

Slide 27

Slide 27 text

A long time ago in a galaxy far, far away...

Slide 28

Slide 28 text

Seriously

Slide 29

Slide 29 text

Ed 1971 ( Ken Thompson )

Slide 30

Slide 30 text

g/re/p

Slide 31

Slide 31 text

g/re/p global regular expression print

Slide 32

Slide 32 text

grep 1973 (Ken Thompson)

Slide 33

Slide 33 text

self.clone! egrep fgrep sed awk lex

Slide 34

Slide 34 text

Perl 1987 ( Larry Wall )

Slide 35

Slide 35 text

Regular Expressions Package 1987 (Henry Spencer)

Slide 36

Slide 36 text

Perl 5 1994

Slide 37

Slide 37 text

PCRE 1994

Slide 38

Slide 38 text

Perl

Slide 39

Slide 39 text

Compatible

Slide 40

Slide 40 text

Regular

Slide 41

Slide 41 text

Expressions

Slide 42

Slide 42 text

Basics

Slide 43

Slide 43 text

Follow Along Rubular (Ruby 1.8 Regex Tester) http://www.rubular.com/ RubyXP (Ruby 1.9 Regex Tester) http://www.rubyxp.com/

Slide 44

Slide 44 text

/^Hello [Ww]orld.?$/

Slide 45

Slide 45 text

Literal Characters

Slide 46

Slide 46 text

Any Letter or Number Including blank space!

Slide 47

Slide 47 text

Metacharacters There are a few

Slide 48

Slide 48 text

[ \ ^ $ . | ? * + ( )

Slide 49

Slide 49 text

// (the construct)

Slide 50

Slide 50 text

Anchor Characters ^ (beginning) $ (end)

Slide 51

Slide 51 text

Character Matchers . (Any Character) \d (Any Number) \s (Any Whitespace) \w (Any Word Character)

Slide 52

Slide 52 text

Quantifiers * (Zero or more) + (One or more) ? (One or None) {n,m} (Only n matches up to m)

Slide 53

Slide 53 text

Quantifiers (part Deux) Changes the number of matches to the left of the quantifier. a* (if there, match a, keep going) a+ (must match a, keep going) a? (if there, match a) a{1,3} (matches a or aa or aaa)

Slide 54

Slide 54 text

Character Classes [] (Matches groups of literal characters) [^] (Don’t match any of these characters)

Slide 55

Slide 55 text

Character Classes (gotcha) [.+*] (Matches a dot, plus or splat) Any meta character inside a character class becomes a literal character

Slide 56

Slide 56 text

Character Classes (part Deux) \w = [A-Za-z0-9_] \W = [^A-Za-z0-9_] \s = [ \t\r\n] The - is a range of literal characters

Slide 57

Slide 57 text

Grouping Combine matches into groups using ( and ) surrounding your matches. (Hello) World Matches Hello World and Hello separately

Slide 58

Slide 58 text

Escaping (from alcatraz) Use \ to turn a metacharacter into a literal . Matches anything \. Matches a period

Slide 59

Slide 59 text

/^Hello [Ww]orld.?$/

Slide 60

Slide 60 text

Break it Down!

Slide 61

Slide 61 text

/^Hello [Ww]orld.?$/

Slide 62

Slide 62 text

/^Hello [Ww]orld.?$/ Beginning End Exact letters H e l l o (including space) Exact letters o r l d Anything Maybe once? Capital or lower-case W

Slide 63

Slide 63 text

/Hello World/ Hello World Hi, Hello World Say Hello World

Slide 64

Slide 64 text

/^Hello World$/ Hello World Hi, Hello World Say Hello World

Slide 65

Slide 65 text

/^Hello World.?$/ Hello World Hello World! Hello, World! Hello, World

Slide 66

Slide 66 text

/^Hello [Ww]orld.?$/ Hello World! Hello world Hello World Hello, world!

Slide 67

Slide 67 text

/^Hello [Ww]orld.?$/

Slide 68

Slide 68 text

/^Hello [Ww]orld.?$/ Beginning End Exact letters H e l l o (including space) Exact letters o r l d Anything Maybe once? Capital or lower-case W

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Good Practices

Slide 71

Slide 71 text

Be Precise

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Don’t Be Greedy!

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

Precision Matches exactly what you want

Slide 76

Slide 76 text

Greed Matches everything you don’t want

Slide 77

Slide 77 text

Precision vs Greed Be as specific as you can! Avoid use of the dot . Avoid greedy quantifiers + or .+

Slide 78

Slide 78 text

Precise! /[0-9]{5}(-[0-9]{4})?/ 12345 12345-6789 867-5309

Slide 79

Slide 79 text

Greedy! /[0-9]+(-[0-9]+)?/ 12345 12345-6789 867-5309

Slide 80

Slide 80 text

ruby (the highlights)

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

Basic Regex if /Bacon/ =~ “Chunky Bacon” puts “Bacon!” end => “Bacon!”

Slide 83

Slide 83 text

Match “Chunky Bacon”.match(/Bacon/).to_s => “Bacon”

Slide 84

Slide 84 text

Match (Part Deux) regex = %r{ Bacon }x regex.match(“Chunky Bacon”).to_s => “Bacon”

Slide 85

Slide 85 text

Gsub “Chunky Bacon”.gsub(/Bacon/, “Foo”) => “Chunky Foo”

Slide 86

Slide 86 text

Options Matching options (usually added after the second /) i - case insensitive o - interpolate once (#{}’s) m - multiline mode (‘.’ matches \n) x - extended mode (ignore whitespace)

Slide 87

Slide 87 text

Options (Extended Dvd Edition) “Chunky Bacon”.match(/bacon/i).to_s => “Bacon”

Slide 88

Slide 88 text

1.9 Only (but possible with Oniguruma gem)

Slide 89

Slide 89 text

POSIX Matchers [:alpha:] = [a-zA-Z] [:alnum:] = [a-zA-Z0-9] Many, many more

Slide 90

Slide 90 text

Arrays? str = “Chunky Bacon” str[/Bacon/] => “Bacon”

Slide 91

Slide 91 text

Assertions (?=) Look Ahead (?!) Negative Look Ahead (?<) Look Behind (?

Slide 92

Slide 92 text

And Many More Checkout Oniguruma Gem

Slide 93

Slide 93 text

Questions?

Slide 94

Slide 94 text

Menu

Slide 95

Slide 95 text

brain.reload assertions

Slide 96

Slide 96 text

Follow Along RubyXP (Ruby 1.9 Regex Tester) http://www.rubyxp.com/ Graffiti (RubyGem) http://rubygems.org/gems/graffiti gem install graffiti

Slide 97

Slide 97 text

brain.reload

Slide 98

Slide 98 text

/^Hello [Ww]orld.?$/

Slide 99

Slide 99 text

/^Hello [Ww]orld.?$/ Beginning End Exact letters H e l l o (including space) Exact letters o r l d Anything Maybe once? Capital or lower-case W

Slide 100

Slide 100 text

Literal Characters

Slide 101

Slide 101 text

Any Character

Slide 102

Slide 102 text

/Chunky Bacon/ Chunky Bacon ChunkyBacon

Slide 103

Slide 103 text

Meta Characters

Slide 104

Slide 104 text

[ \ ^ $ . | ? * + ( )

Slide 105

Slide 105 text

Grouping

Slide 106

Slide 106 text

Grouping Hello world Hello (world) Hello world world

Slide 107

Slide 107 text

Quantifiers

Slide 108

Slide 108 text

Quantifiers * (Zero or more) + (One or more) ? (One or None) {n,m} (Only n matches up to m)

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

Assertions

Slide 111

Slide 111 text

Lookahead

Slide 112

Slide 112 text

Lookbehind

Slide 113

Slide 113 text

Lookaround

Slide 114

Slide 114 text

Zero-Width Assertions

Slide 115

Slide 115 text

IF - ELSE

Slide 116

Slide 116 text

/^Hello [Ww]orld.?$/

Slide 117

Slide 117 text

/^Hello [Ww]orld.?$/ Start Here

Slide 118

Slide 118 text

/^Hello [Ww]orld.?$/ Start Here Walk this way

Slide 119

Slide 119 text

Lookaround

Slide 120

Slide 120 text

Positive

Slide 121

Slide 121 text

Negative

Slide 122

Slide 122 text

Positive = True

Slide 123

Slide 123 text

Negative = False

Slide 124

Slide 124 text

Break it Down!

Slide 125

Slide 125 text

Lookahead

Slide 126

Slide 126 text

Lookahead /b(?=a)/ <- Positive /b(?!a)/ <- Negative

Slide 127

Slide 127 text

Lookahead (positive) /b(?=a)/ bacon beacon

Slide 128

Slide 128 text

Lookahead (positive) /b(?=a)/ Match b

Slide 129

Slide 129 text

Lookahead (positive) /b(?=a)/ Match b If (true)

Slide 130

Slide 130 text

Lookahead (positive) /b(?=a)/ Match b If (true) Followed by a

Slide 131

Slide 131 text

Lookahead (positive) /b(?=a)/ bacon beacon

Slide 132

Slide 132 text

Zero-Width Assertions

Slide 133

Slide 133 text

Lookbehind

Slide 134

Slide 134 text

Lookbehind /(?<=o)l/ <- Positive /(?

Slide 135

Slide 135 text

Lookbehind (positive) /(?<=o)l/ pool peel

Slide 136

Slide 136 text

Lookbehind (positive) /(?<=o)l/ If (true)

Slide 137

Slide 137 text

Lookbehind (positive) /(?<=o)l/ If (true) Preceded by o

Slide 138

Slide 138 text

Lookbehind (positive) /(?<=o)l/ Match L If (true) Preceded by o

Slide 139

Slide 139 text

No content

Slide 140

Slide 140 text

Examples

Slide 141

Slide 141 text

validates_length_of :title, minimum: 2

Slide 142

Slide 142 text

validates :title, length: { minimum: 2 }

Slide 143

Slide 143 text

/(?=.{2,}).+/

Slide 144

Slide 144 text

/(?=.{2,}).+/ T i t l e

Slide 145

Slide 145 text

/(?=.{2,}).+/ T i t l e

Slide 146

Slide 146 text

/(?=.{2,}).+/ T i t l e

Slide 147

Slide 147 text

/(?=.{2,}).+/ T i t l e

Slide 148

Slide 148 text

/(?=.{2,}).+/ T i t l e

Slide 149

Slide 149 text

/(?=.{2,}).+/ T i t l e

Slide 150

Slide 150 text

/(?=.{2,}).+/ T i t l e

Slide 151

Slide 151 text

MOAR?

Slide 152

Slide 152 text

number_to_currency

Slide 153

Slide 153 text

/(\d)(?=(\d{3})+(?!\d))/

Slide 154

Slide 154 text

No content

Slide 155

Slide 155 text

“1000000.00”.gsub /(\d)(?=(\d{3})+(?!\d))/, ‘\1,’ 1,000,000.00

Slide 156

Slide 156 text

1000000.00 /(\d)(?=(\d{3})+(?!\d))/

Slide 157

Slide 157 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number 1000000.00

Slide 158

Slide 158 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) 1000000.00

Slide 159

Slide 159 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead 1000000.00

Slide 160

Slide 160 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more Followed by non-number 1000000.00

Slide 161

Slide 161 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1000000.00 Followed by non-number

Slide 162

Slide 162 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1000000.00 Followed by non-number

Slide 163

Slide 163 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1000000.00 Followed by non-number

Slide 164

Slide 164 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 165

Slide 165 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number 1,000000.00

Slide 166

Slide 166 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) 1,000000.00

Slide 167

Slide 167 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00

Slide 168

Slide 168 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 169

Slide 169 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 170

Slide 170 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 171

Slide 171 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number FALSE

Slide 172

Slide 172 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 173

Slide 173 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 174

Slide 174 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number FALSE

Slide 175

Slide 175 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 176

Slide 176 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 177

Slide 177 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 178

Slide 178 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000000.00 Followed by non-number

Slide 179

Slide 179 text

/(\d)(?=(\d{3})+(?!\d))/ Match a number If (true) Look for 3 numbers ahead at least once or more 1,000,000.00 Followed by non-number

Slide 180

Slide 180 text

No content

Slide 181

Slide 181 text

Resources Rubular (Ruby 1.8 Regex Tester) http://www.rubular.com/ Ruby Expressions http://rubyexpressions.com http://regular-expressions.info

Slide 182

Slide 182 text

fin!

Slide 183

Slide 183 text

Thank you! Windy City Rails Organizers! Derek Weathersbee (Franchise Font) http://vurl.me/AZSG Raph Levien (Inconsolata Font) http://vurl.me/AZSH

Slide 184

Slide 184 text

aaron.inspect “web” => martinisoftware.com “twitter” => @martinisoft “github” => martinisoft “spkr8” => spkr8.com/t/8317