Try it yourself with our free Regex Tester tool — runs entirely in your browser, no signup needed.

How to Use regex to replace in Ruby

How to use regex to replace in Ruby

Regular expressions (regex) are a powerful tool for text manipulation, and Ruby provides an excellent implementation through its Regexp and String classes. In this guide, we'll explore how to use regex to replace text in Ruby, covering the basics, handling edge cases, and providing performance tips.

Quick Example

Here's a minimal example that replaces all occurrences of "old" with "new" in a string:

require 'regexp'

text = "The old car is old and needs to be replaced."
pattern = /old/
replacement = "new"

new_text = text.gsub(pattern, replacement)
puts new_text # Output: "The new car is new and needs to be replaced."

This code uses the gsub method, which replaces all occurrences of the pattern in the string.

Step-by-Step Breakdown

Let's break down the code:

  1. require 'regexp': This line imports the Regexp class, which provides the regex functionality in Ruby.
  2. text = "The old car is old and needs to be replaced.": This line defines the input string.
  3. pattern = /old/: This line defines the regex pattern to match. In this case, it's a simple string "old".
  4. replacement = "new": This line defines the replacement string.
  5. new_text = text.gsub(pattern, replacement): This line uses the gsub method to replace all occurrences of the pattern in the string. The gsub method returns a new string with the replacements made.
  6. puts new_text: This line prints the resulting string to the console.

Handling Edge Cases

Empty/Null Input

When dealing with empty or null input, you should check for these cases before attempting to perform the replacement:

text = nil
pattern = /old/
replacement = "new"

if text && !text.empty?
  new_text = text.gsub(pattern, replacement)
  puts new_text
else
  puts "Input is empty or null"
end

Invalid Input

If the input string contains invalid characters, such as newline characters or non-ASCII characters, you may need to preprocess the input before performing the replacement:

text = "Hello\nWorld"
pattern = /old/
replacement = "new"

text = text.gsub("\n", ' ') # Replace newline characters with spaces
new_text = text.gsub(pattern, replacement)
puts new_text

Large Input

When dealing with large input strings, you may want to consider using a streaming approach to avoid loading the entire string into memory:

require 'stringio'

text = StringIO.new("The old car is old and needs to be replaced.")
pattern = /old/
replacement = "new"

text.rewind
new_text = ""
while line = text.gets
  new_text << line.gsub(pattern, replacement)
end
puts new_text

Unicode/Special Characters

When dealing with Unicode or special characters, you may need to use Unicode-aware regex patterns:

text = "Hello, Sérgio!"
pattern = / Sérgio/ # Note the Unicode character "é"
replacement = "World"

new_text = text.gsub(pattern, replacement)
puts new_text

Common Mistakes

1. Using sub instead of gsub

The sub method only replaces the first occurrence of the pattern, whereas gsub replaces all occurrences:

text = "The old car is old and needs to be replaced."
pattern = /old/
replacement = "new"

new_text = text.sub(pattern, replacement) # WRONG - only replaces first occurrence
new_text = text.gsub(pattern, replacement) # CORRECT - replaces all occurrences

2. Not handling nil or empty input

Failing to check for nil or empty input can result in errors or unexpected behavior:

text = nil
pattern = /old/
replacement = "new"

new_text = text.gsub(pattern, replacement) # WRONG - raises error
if text && !text.empty?
  new_text = text.gsub(pattern, replacement)
  puts new_text
end # CORRECT - handles nil or empty input

3. Not using Unicode-aware patterns

Failing to use Unicode-aware patterns can result in incorrect matches or replacements:

text = "Hello, Sérgio!"
pattern = / Sergio/ # WRONG - does not match Unicode character "é"
pattern = / Sérgio/ # CORRECT - matches Unicode character "é"

Performance Tips

1. Use gsub instead of sub when replacing multiple occurrences

The gsub method is optimized for replacing multiple occurrences of a pattern, whereas sub is optimized for replacing a single occurrence:

text = "The old car is old and needs to be replaced."
pattern = /old/
replacement = "new"

new_text = text.gsub(pattern, replacement) # FASTER - uses optimized gsub method

2. Use Regexp.union to combine multiple patterns

When replacing multiple patterns, use Regexp.union to combine the patterns into a single regex:

pattern1 = /old/
pattern2 = /new/
replacement = "updated"

text = "The old car is new and needs to be replaced."
new_text = text.gsub(Regexp.union(pattern1, pattern2), replacement) # FASTER - uses combined regex

FAQ

Q: What is the difference between sub and gsub?

A: sub replaces the first occurrence of the pattern, whereas gsub replaces all occurrences.

Q: How do I handle nil or empty input?

A: Check for nil or empty input before attempting to perform the replacement.

Q: How do I use Unicode-aware patterns?

A: Use Unicode-aware regex patterns, such as / Sérgio/, to match Unicode characters.

Q: What is the performance difference between sub and gsub?

A: gsub is optimized for replacing multiple occurrences of a pattern, whereas sub is optimized for replacing a single occurrence.

Q: Can I use regex to replace text in a file?

A: Yes, you can use the File class to read and write files, and the gsub method to replace text in the file contents.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp