Examples of Ruby Regular Expressions Examples

Explore practical examples of Ruby Regular Expressions to enhance your programming skills.
By Jamie

Understanding Ruby Regular Expressions

Regular expressions (regex) are powerful tools used in programming for pattern matching within strings. In Ruby, regex is integral for tasks such as validation, searching, and manipulating text. Below are three practical examples of Ruby Regular Expressions that demonstrate their versatility and utility.

1. Validating Email Addresses

Validating email formats is a common use case for regular expressions. This example checks whether a given string conforms to a standard email format.

def valid_email?(email)
  regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
  !!(email =~ regex)
end

puts valid_email?("example@test.com")  # Output: true
puts valid_email?("invalid-email")      # Output: false

In this example, we define a method valid_email? that takes an email string as an argument. The regex pattern matches the components of a valid email address, ensuring it includes characters before and after the @ symbol, as well as a domain. The method returns true if the email matches the regex; otherwise, it returns false.

Notes:

  • The regex can be modified to accommodate more complex email formats if necessary.

2. Extracting Dates from Text

This example demonstrates how to extract dates from a text string. We will capture dates in the format MM/DD/YYYY.

def extract_dates(text)
  regex = /\b(\d{1,2}\/\d{1,2}\/\d{4})\b/
  text.scan(regex).flatten
end

text = "The deadlines are 12/01/2023 and 01/15/2024."
puts extract_dates(text)  # Output: ["12/01/2023", "01/15/2024"]

In this example, the extract_dates method uses the scan method to find all occurrences of dates in the specified format within the input text. The regex pattern looks for two digits (for month), two digits (for day), and four digits (for year), ensuring that they are separated by slashes.

Notes:

  • You can adjust the regex to match different date formats (e.g., YYYY-MM-DD) by modifying the pattern accordingly.

3. Replacing Multiple Whitespace Characters

Regular expressions can also be used to clean up text by replacing multiple consecutive whitespace characters with a single space.

def clean_text(text)
  regex = /\s+/  # Matches one or more whitespace characters
  text.gsub(regex, ' ').strip
end

sample_text = "This   is   a   sample   text."
puts clean_text(sample_text)  # Output: "This is a sample text."

In this example, the clean_text method replaces multiple whitespace characters in a given string with a single space and removes any leading or trailing whitespace. The gsub method is utilized here to perform the replacement, while strip ensures clean output.

Notes:

  • This regex can be extended to remove other unwanted characters by modifying the pattern to include them.

By understanding and implementing these examples of Ruby Regular Expressions, you can effectively perform text validation, extraction, and manipulation in your Ruby applications.