RegExes in Ruby — A Brief Summary

Image for post
Image for post
Me when I see a RegEx

This is a brief overview of the Ruby class Regexp. However, regexes other languages (JavaScript, Python, etc) are similar.

There are 3 things we need to recognize to start understanding regexes.

What if we need to match the special characters?

Matching

Image for post
Image for post
We want {4} digits \d followed by -. All of that {3} times, ending with {4} digits \d.
dash_numbers looks for a dash followed by {4} digits \d
.match? is trying to find {16} digits \d in a row, and will just return “true” or “false”

Changing

Image for post
Image for post
chamelion.gsub(‘green’, new_color)
In both lines 3 and 5, we are just trying to match any lower-case vowels

Assigning Variables!

Image for post
Image for post
This is quite exciting
.scan returns every chunk of {4} digits \d

How else can regexes be useful?

Image for post
Image for post

What can regexes help you do?

Potential Problems

Resources

Written by

Full Stack Web Developer, former English as a foreign language teacher and volleyball coach. All views and opinions are my own.

Get the Medium app