Ruby Regex

Objetives

Regex

Regular Expressions

# Literals

# ===== Rugular =====
bark
# ===== Test =====
# Match this String
bark
# Don't match these strings
baa
bellow
boom
# Alternation

# ===== Rugular =====
cat|dog
# ===== Test =====
# Match these strings
cat
dog
# Character Sets

# ===== Rugular =====
[chr]at
# ===== Test =====
# Match these strings
cat
hat
rat
# Don't match these strings
eay
mat
sath
# Wildcards

# ===== Rugular =====
....\.
# ===== Test =====
# Match these strings
bear.
lion.
orca.
# Don't match these strings (no poner esta linea en este ejemplo)
mouse
koala
snail
# Ranges

# ===== Rugular =====
[c-e][l-u][b-k]
# ===== Test =====
# Match these strings
cub
dog
elk
# Don't match these strings
ape
cow
ewe

Shorthand Character Classes

# Shorthand Character Classes

\w  # [A-Za-z0-9_]
\d  # [0-9]
\s  # Any whitespace character

# Negative
\W  # [^A-Za-z0-9_]
\D  # [^0-9]
\S  # Any non-whitespace character
# Example

# ===== Rugular =====
\d\s\w\w\w\w\w\w
# ===== Test =====
# Match these strings
5 sloths
8 llamas
7 hyenas
# Don't match these strings (no poner esta linea en este ejemplo)
one bird
two owls

Grouping

# Grouping

# ===== Rugular =====
I love (cat|dog)
# ===== Test =====
# Match these strings
I love cat
I love dog

Quantifiers

# Quantifiers Fixed

# ===== Rugular =====
\d\s\w{6}
# ===== Test =====
# Match these strings
5 sloths
8 llamas
7 hyenas
# Don't match these strings (no poner esta linea en este ejemplo)
one bird
two owls
# Quantifiers Fixed Range

# ===== Rugular =====
squea{3,5}k
# ===== Test =====
# Match these strings
squeaaak
squeaaaak
squeaaaaak
# Don't match these strings (no poner esta linea en este ejemplo)
squeak
squeaak
squeaaaaaak
# Quantifiers Optional

# ===== Rugular =====
\d\sducks?\sfor\sadoption\?  # Solition 1
\d\s\w{4}s?\s\w{3}\s\w{8}\?  # Solution 2
# ===== Test =====
# Match these strings
1 duck for adoption?
5 ducks for adoption?
7 ducks for adoption?
# Quantifiers 0 or more

# ===== Rugular =====
meo*w
# ===== Test =====
# Match these strings
mew
meow
meooow
meoooooooooooow
# Quantifiers 1 or more

# ===== Rugular =====
meo+w
# ===== Test =====
# Match these strings
meow
meooow
meoooooooooooow
# Exercise

# ===== Rugular =====
# ?
# Match these strings
hoot
hoooooot
hooooooooooot
# Don't match these strings
hot
hoat
hoo

Anchors

# Anchors

# ===== Rugular =====
^hoo+t$
# Match these strings
hoot
hoooooot
hooooooooooot
hooooooooooot!
yahoooot
# Don't match these strings
hot
hoat
hoo
# Exercise 

# ===== Rugular =====
# ?
# Match these strings
perro
pero
poberto
PO
po
# Don't match these strings
p0b3rt0
p0 3rt0

Methods

# Match method

puts 'abc ac adc abbbc'.match(/ab*c/)

# string argument is treated the same as a regexp
puts 'abc ac adc abbbc'.match('a.*d')

# second argument specifies starting location to search for a match
puts 'abc ac adc abbbc'.match(/ab*c/, 7)

# captured groups
puts m = 'abc ac adc abbbc'.match(/a(.*)d(.*a)/)
puts m.captures
# Scan Method

'abc ac adc abbbc'.scan(/ab*c/)
# ["abc", "ac", "abbbc"]
 
'abc ac adc abbbc'.scan(/ab+c/)
# ["abc", "abbbc"]
 
'par spar apparent spare part'.scan(/\bs?pare?\b/)
# ["par", "spar", "spare"]
# Gsub Method

p "    a b c".gsub(/ /, '_')
# "____a_b_c"

'good,bad 42,24'.gsub(/(\w+),(\w+)/, '\2,\1')
# "bad,good 24,42"

'[52] apples [and] [31] mangoes'.gsub(/\[(\d+)\]/, '\1')
# "52 apples [and] 31 mangoes"

Ruby Regex

By Paulo Tijero

Ruby Regex

  • 77