0Pricing
Ruby Academy · Lesson

Regex Literals

Pattern basics.

Regex Literals is a free Ruby Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Regular Expression?

A regular expression (regex) is a pattern used to match text. In Ruby, regex is a first-class object of class Regexp.

You use regex to search, validate, and extract parts of strings.

  • Match a word, digit, or symbol
  • Validate emails, phone numbers
  • Extract pieces from messy text
pattern = /hello/
puts pattern.class
puts(pattern.match?('hello world'))

Regex Literals with Slashes

The most common way to write a regex in Ruby is between two forward slashes: /pattern/.

This is called a regex literal. Everything between the slashes is the pattern.

ruby = /ruby/
puts ruby.class
puts(ruby.match?('I love ruby'))
puts(ruby.match?('I love python'))

The =~ Match Operator

The =~ operator tests a string against a regex. It returns the index of the first match, or nil if there is no match.

puts('hello world' =~ /world/)
puts(('hello world' =~ /xyz/).inspect)

match? for a Boolean Answer

When you only need a yes/no answer, use match?. It returns true or false and is faster because it does not build a MatchData object.

puts(/\d/.match?('abc123'))
puts(/\d/.match?('abcdef'))

Character Classes

A character class in square brackets matches any one character from a set.

  • [abc] matches a, b, or c
  • [a-z] matches any lowercase letter
  • [0-9] matches any digit
puts(/[aeiou]/.match?('sky'))
puts(/[aeiou]/.match?('tree'))
puts(/[0-9]/.match?('room 7'))

Shorthand Classes

Ruby provides shorthand escapes for common character classes:

  • \d a digit, \D a non-digit
  • \w a word char (letters, digits, underscore)
  • \s whitespace, \S non-whitespace
puts(/\d\d/.match?('age 42'))
puts(/\w+/.match?('hello_99'))
puts(/\s/.match?('no_space'))

The Dot and Quantifiers

The dot . matches any single character (except newline). Quantifiers control repetition:

  • * zero or more
  • + one or more
  • ? zero or one
  • {2,4} between 2 and 4 times
puts(/ab+c/.match?('abbbc'))
puts(/colou?r/.match?('color'))
puts(/\d{3}/.match?('id 451'))

Alternation with the Pipe

The pipe | means OR. The pattern cat|dog matches either word.

Use parentheses to group an alternation inside a larger pattern.

pet = /cat|dog/
puts(pet.match?('I have a dog'))
puts(pet.match?('I have a fish'))
puts(/^(yes|no)$/.match?('yes'))

Case-Insensitive Flag

Add the i flag after the closing slash to make matching case-insensitive.

Other useful flags include m (dot matches newline) and x (extended, ignore whitespace).

puts(/ruby/i.match?('I love RUBY'))
puts(/ruby/.match?('I love RUBY'))

Building Regex from Strings

You can build a regex at runtime with Regexp.new. Use Regexp.escape to safely include user input that may contain special characters.

word = 'price'
pattern = Regexp.new(word)
puts(pattern.match?('the price is high'))
puts(Regexp.escape('a.b*c'))

Anchors Preview

Anchors match positions, not characters:

  • ^ start of a line
  • $ end of a line
  • \A start of the string
  • \z end of the string

You will study these in depth later.

puts(/^Hello/.match?('Hello there'))
puts(/world$/.match?('hello world'))

Quick Check

What does the =~ operator return when a regex matches a string?

Recap: Regex Literals

You learned the foundations of Ruby regex:

  • Regex literals are written between /slashes/
  • =~ returns an index, match? returns true/false
  • Character classes [ ] and shorthands \d \w \s
  • Quantifiers * + ? {n,m} and alternation |
  • The i flag makes matching case-insensitive

Next you will capture the matched pieces.

puts(/\w+@\w+\.\w+/i.match?('USER@Mail.com'))

Frequently asked questions

Is the “Regex Literals” lesson free?

Yes — the full text of “Regex Literals” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Regex Literals”?

Pattern basics. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Regex Literals” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Regex Literals
  2. Matching and Capturing
  3. Substitution
  4. Named Groups and Anchors
← Back to Ruby Academy