Regex in Ruby - The Beginner's Guide

Regex in Ruby - The Beginner's Guide

Regex (Regular Expressions) is not unique to Ruby (many languages have some form of regex), but we’ll focus on Ruby’s version here. Ruby regular expressions (ruby regex for short) help you find specific patterns inside strings, with the intent of extracting data for further processing.

If you’re new to Ruby (or any programming language), you may have come across these strange bits of coding when you search stack overflow for answers to life’s greatest questions — like how to count the number of sentences in a string:

string.strip.split(/\w[?!.]/)

You may be wondering — what is this code within a code?

It’s called Regex — short for Regular Expressions! It’s not unique to Ruby (many languages have some form of regex), but we’ll focus on Ruby’s version here.

Regex code is used for specifying a certain search pattern of characters to be matched in a string (like finding words that end with -ing, or places in a string where a space follows a punctuation mark). Once we use this search pattern, we can pull out those matches or manipulate them in some way.

An example of a real-world use for regex would be validating an email address is entered correctly — it has a string of before an @ sign, and a string before a .com (or some variation) — but there are an infinite number of ways you may find it useful in your own code!

Regex Decoded

First things first, regex code goes between two forward slashes to differentiate it from the rest of your code:

/ *fancy code stuff* /

Now, the interesting part — there are a few major types of regular expressions that can go inside these slashes:

  1. Anchors
^     Start of a line              $     End of a line

\A    Start of a string            \z    End of a string
$     End of a string, or line
\b    Any word boundary            
\<    Start of a word              \>    End of a word

Anchors tell the search where to start or stop. For example:

/\A *fancy code stuff* /

says “start searching for ___ (whatever you put next in the regex) at the start of every string.”

Notice the clever way \A (the first letter of the alphabet) denotes the start of a string, and \z (the last letter) denotes the end of a string

2. Groups and ranges

Groups and ranges can include numbers or letters, and tell the search what characters you’re looking to match:

[abc]         Any single character (a, b or c)

[^abc]        Excluding any single character (a, b , or c)
[a-x]         Any lowercase character between a-x
[A-T]         Any uppercase character between A-T
[a-zA-Z]      Any character between a-z or A-Z
[0-7]         Any number from 0 through 7
(a|b)         A or b (but not both)

So if we had:

/ \<[a-m] /

…the search is saying: “go to the start of every word, and find those that are any letter between lowercase a and m”.

regex ruby-on-rails programming ruby developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Ruby on Rails Developer | Hire RoR Developer

#1 Ruby on Rails development company. Hire Ruby on rails developer or a team to build secure, scalable and complex web solutions with a quick turnaround time.

"Rewrite your Software," says Creator of Ruby on Rails

David Heinemeier Hansson is the creator of Ruby on Rails, co-founder & CTO of Basecamp, best-selling author, Le Mans class-winning race car driver, family man, frequent podcast guest, and inspirational conference speaker.

Hire Ruby on Rails Developers

![Hire Ruby on Rails Developers](https://semidotinfotech.com/storage/uploads/service/hire-ruby-on-rails-developers.jpg "Hire Ruby on Rails Developers") [Hire ruby on rails developers](https://semidotinfotech.com/hire-ruby-on-rails-developers...

Why Ruby on Rails(RoR) is still a good choice in 2020.

Ruby on Rails in 2020: Reasons why RoR is still a viable technology in 2020 to bring your business app ideas to life with examples.

How to Deploy a Ruby on Rails App on Heroku

The most satisfying thing beyond building something is to make it available to the world. Heroku is great for beginners because it’s a free and “simple” push-to-deploy system.