Ruby Tutorial
Ruby CGI
Ruby Advanced
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:
regex = /pattern/
Or you can use the Regexp
class:
regex = Regexp.new('pattern')
match
method:puts 'Hello, world!' =~ /world/ # Outputs: 7 (index of the match) puts 'Hello, world!' =~ /World/ # Outputs: nil (no match)
i
after the regular expression to make it case-insensitive:puts 'Hello, world!' =~ /world/i # Outputs: 7 (match found, despite case difference)
.
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")
[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.^
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.
Matching patterns in Ruby with regex:
Use the =~
operator to match patterns in strings.
pattern = /pattern/ result = "This is a pattern match" =~ pattern
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
Ruby regex character classes: Character classes match specific characters or ranges.
pattern = /[aeiou]/ result = "Hello".match(pattern)
Quantifiers in Ruby regular expressions: Quantifiers specify the number of occurrences.
pattern = /\d{2,4}/ result = "123".match(pattern)
Anchors and boundaries in Ruby regex: Anchors specify the position in the string.
pattern = /\A\d{3}\z/ result = "123".match(pattern)
Lookahead and lookbehind in Ruby regex: Lookahead and lookbehind assert conditions without consuming characters.
pattern = /(?<=@)\w+/ result = "user@example.com".match(pattern)
Using regex with String methods in Ruby:
Apply regex with match
, scan
, and other String methods.
pattern = /\d+/ result = "123abc456".scan(pattern)
Ruby regex escape characters:
Escape characters with special meaning using \
.
pattern = /\d\.\d/ result = "3.14".match(pattern)
Ruby regex global matching:
Use the String#scan
method for global matching.
pattern = /\d+/ result = "123abc456".scan(pattern)
Replacing text with regex in Ruby:
Replace matched patterns using the gsub
method.
pattern = /\d+/ result = "123abc456".gsub(pattern, 'X')
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)
Debugging regex patterns in Ruby:
Debug using tools like Rubular or the Regexp#inspect
method.
pattern = /\d{3}/ puts pattern.inspect