Ruby Regular Expressions

Regular expressions, often simply called regex, are a powerful tool for manipulating text. They are patterns used to match character combinations in strings. In Ruby, regular expressions are objects created with the /.../ and %r{...} literals, and the Regexp.new constructor.

Here is a basic tutorial on regular expressions in Ruby:

  • Creating Regular Expressions: You can create a regular expression like this:
regex = /pattern/

Or you can use the Regexp class:

regex = Regexp.new('pattern')
  • Basic Matching: To see if a string matches a regular expression, you can use the match method:
puts 'Hello, world!' =~ /world/  # Outputs: 7 (index of the match)
puts 'Hello, world!' =~ /World/  # Outputs: nil (no match)
  • Case Insensitivity: You can add an i after the regular expression to make it case-insensitive:
puts 'Hello, world!' =~ /world/i  # Outputs: 7 (match found, despite case difference)
  • Special Characters: There are many special characters that have different meanings in regular expressions. Here are a few:
  • . matches any single character except a newline.
  • * matches zero or more of the preceding character.
  • + matches one or more of the preceding character.
  • ? matches zero or one of the preceding character.
  • {n} matches exactly n of the preceding character.
  • {n,} matches n or more of the preceding character.
  • {n,m} matches at least n and at most m of the preceding character.

Here are some examples:

puts 'aaa' =~ /a*/  # Outputs: 0 (matches "aaa")
puts 'aaa' =~ /a+/  # Outputs: 0 (matches "aaa")
puts 'aaa' =~ /a?/  # Outputs: 0 (matches "a")
puts 'aaa' =~ /a{3}/  # Outputs: 0 (matches "aaa")
puts 'aaaaaa' =~ /a{3,}/  # Outputs: 0 (matches "aaaaaa")
puts 'aaaaaa' =~ /a{3,5}/  # Outputs: 0 (matches "aaaaa")
  • Character Classes: You can use character classes to match any character out of a set of characters:
  • [abc] matches any single character that is a, b, or c.
  • [^abc] matches any single character that is not a, b, or c.
  • [a-z] matches any single lowercase letter.
  • [A-Z] matches any single uppercase letter.
  • [0-9] or \d matches any single digit.
  • \D matches any character that is not a digit.
  • \w matches any word character (equivalent to [a-zA-Z0-9_]).
  • \W matches any character that is not a word character.
  • \s matches any whitespace character (spaces, tabs, line breaks).
  • \S matches any character that is not a whitespace character.
  • Anchors: Anchors are used to specify the position of the match:
  • ^ specifies the start of the line.
  • $ specifies the end of the line.
puts 'Hello, world!' =~ /^Hello/  # Outputs: 0 (match at the start of the string)
puts 'Hello, world!' =~ /world!$/ # Outputs: 7 (match at the end of the string)

This is a very basic introduction to regular expressions in Ruby. Regular expressions are a large and complex topic, but this should be enough to get you started.

  1. Matching patterns in Ruby with regex: Use the =~ operator to match patterns in strings.

    pattern = /pattern/
    result = "This is a pattern match" =~ pattern
    
  2. Capturing groups in Ruby regex: Capture parts of a pattern using parentheses.

    pattern = /(\d{2})-(\d{2})-(\d{4})/
    match = "01-01-2023".match(pattern)
    puts match[1]  # Day
    puts match[2]  # Month
    puts match[3]  # Year
    
  3. Ruby regex character classes: Character classes match specific characters or ranges.

    pattern = /[aeiou]/
    result = "Hello".match(pattern)
    
  4. Quantifiers in Ruby regular expressions: Quantifiers specify the number of occurrences.

    pattern = /\d{2,4}/
    result = "123".match(pattern)
    
  5. Anchors and boundaries in Ruby regex: Anchors specify the position in the string.

    pattern = /\A\d{3}\z/
    result = "123".match(pattern)
    
  6. Lookahead and lookbehind in Ruby regex: Lookahead and lookbehind assert conditions without consuming characters.

    pattern = /(?<=@)\w+/
    result = "user@example.com".match(pattern)
    
  7. Using regex with String methods in Ruby: Apply regex with match, scan, and other String methods.

    pattern = /\d+/
    result = "123abc456".scan(pattern)
    
  8. Ruby regex escape characters: Escape characters with special meaning using \.

    pattern = /\d\.\d/
    result = "3.14".match(pattern)
    
  9. Ruby regex global matching: Use the String#scan method for global matching.

    pattern = /\d+/
    result = "123abc456".scan(pattern)
    
  10. Replacing text with regex in Ruby: Replace matched patterns using the gsub method.

    pattern = /\d+/
    result = "123abc456".gsub(pattern, 'X')
    
  11. Validating input with regex in Ruby: Ensure input adheres to a specific pattern.

    pattern = /\A\d{3}-\d{2}-\d{4}\z/
    result = "123-45-6789".match(pattern)
    
  12. Debugging regex patterns in Ruby: Debug using tools like Rubular or the Regexp#inspect method.

    pattern = /\d{3}/
    puts pattern.inspect