Regular Expressions in Ruby

Regular expressions, also known as regex, are patterns used to search, modify, and manipulate text in Ruby. They are a powerful tool for working with textual data efficiently and accurately.

Creating regular expressions: Link to heading

  • They are defined between two forward slashes (/).
  • They can contain a combination of special characters and literals.
  • Special characters have a predefined meaning within the regular expression.

Basic characters Link to heading

Expression meaning
. (Dot): Matches any single character (except newline).
[] (Brackets): Character range. Example: [az] matches any lowercase letter from a to z.
\w (Letter or number): Matches letters, numbers and underscore (_).
\W (Not letter or number): Matches any character that is not a letter, number, or underscore.
\s (Blank): Matches spaces, tabs, carriage returns, and line breaks.
\S (Non-whitespace): Matches any character that is not a whitespace.
\d (Digit): Matches any digit from 0 to 9.
\D (Non-digit): Matches any character that is not a digit from 0 to 9.

Special metacharacters Link to heading

Expression meaning
^ (Start): Matches the start of the string.
$ (End): Matches the end of the string.
\b (Word edge): Matches the beginning or end of a word.
* (Zero or more): Repeats the character that precedes it zero or more times.
+ (One or more): Repeats the character that precedes it one or more times.
? (Zero or one): Repeats the character that precedes it zero or one time.
| (Alternative): Matches any of the expressions that precede or follow it.
() (Group): Groups an expression to treat it as a single unit.
{m,n} (Specific repetition): Repeats the character or group that precedes it a minimum of m times and a maximum of n times.

Example: Link to heading

is_gmail_regex = /\w+@gmail.com/

Pattern Matching: Link to heading

  • The match method can be used to check if a string matches a regular expression.
  • The match method returns a MatchData object if there is a match, or nil if there is not.

Example: Link to heading

"john@gmail.com".match(is_gmail_regex) # => <MatchData "john@gmail.com">
"john@gmail".match(is_gmail_regex) # => nil

Character classes: Link to heading

  • There are different character classes to simplify writing regular expressions.

Example: Link to heading

number_regex = /\d+/
"21354".match(number_regex) # => <MatchData "21354">
"text".match(number_regex) # => nil

Quantifiers: Link to heading

  • They are used to indicate the number of times a character or pattern should be repeated.

Example: Link to heading

# Search for a word followed by a space and search for a letter
/\w+ \w/ =~ "Hello world" # => 0

# Search for a word followed by two or more spaces and then a letter
/\w+ \s+\w/ =~ "Hello  world" # => 0

Grouping: Link to heading

  • Parentheses can be used to group parts of a regular expression.

Example: Link to heading

# Search for a date in dd/mm/yyyy format
/\d{2}\/\d{2}\/\d{4}/ =~ "12/03/2024" # => 0

# Searches for a date in dd/mm/yyyy format, where dd and mm can be one or two digits
/\d{1,2}\/\d{1,2}\/\d{4}/ =~ "12/03/2024" # => 0

Logical operators: Link to heading

  • Logical operators can be used to combine different regular expressions.

Example: Link to heading

# Search for an email address that can be from Gmail or Hotmail

/(gmail|hotmail)\.com/ =~ "john@gmail.com" # => 0
/(gmail|hotmail)\.com/ =~ "john@hotmail.com" # => 0

In summary, regular expressions are a powerful tool for working with text in Ruby. Their flexibility and expressiveness make them a fundamental part of the language for tasks such as data search and validation, information extraction, and text transformation.

Keep learning about regular expressions to make the most of their potential in your programs!

Additional Tips: Link to heading

  • You can use the rubular tool to test and debug your regular expressions.
  • You can use the rexml library to work with XML.
  • You can use the nokogiri library to work with HTML.

Keep experimenting with regular expressions to create more robust and versatile programs!

Here are some additional examples of how regular expressions can be used: Link to heading

  • Validate a user’s input
  • Extract information from a web page
  • Format text
  • Find and replace text
  • Generate code
  • Translate languages
  • Analyze data

The possibilities are endless!

<< Ranges in Ruby Procs and Lambdas in Ruby >>