Ad space (banner)
๐Ÿ’ŽRuby Lessons
Lesson 32 / 68

Intro to Regular Expressions (Pattern Matching)

In this lesson you will learn the basics of regular expressions in Ruby so you can validate and extract patterned strings such as email addresses and phone numbers. This is for anyone searching for "Ruby regular expressions how to use".

A regular expression is a special notation that describes the shape of a string. You can specify a pattern such as "three digits, a hyphen, then four digits", check whether text matches it, and pull out the matching part. Ruby writes a regular expression between / /.

The sample code uses a phone-number pattern and pulls just that part out of the text with .match(). The =~ operator is another characteristically Ruby way of matching.

It looks like nothing but punctuation at first, but it is a powerful tool that pays off in practice, for example when validating email formats. A frequent beginner trip-up is that Ruby has several ways to match (=~, match) and it is not obvious which to reach for.

Learn regular expressions once and the skill transfers to most other programming languages. They are used widely in real validation work such as checking form input.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Ruby directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)