Regular expressions (regex) are a powerful tool for matching patterns in text. They are used for searching, editing, and manipulating text. In Ruby, regular expressions are objects of the Regexp class.

Key Concepts

  1. Basic Syntax

  • Literal Characters: Matches the exact characters in the string.
  • Metacharacters: Characters with special meanings (e.g., . matches any character except a newline).

  1. Character Classes

  • Square Brackets []: Matches any one of the characters inside the brackets.
  • Ranges: [a-z] matches any lowercase letter.
  • Negation: [^a-z] matches any character that is not a lowercase letter.

  1. Predefined Character Classes

  • \d: Matches any digit.
  • \D: Matches any non-digit.
  • \w: Matches any word character (alphanumeric + underscore).
  • \W: Matches any non-word character.
  • \s: Matches any whitespace character.
  • \S: Matches any non-whitespace character.

  1. Quantifiers

  • *: Matches 0 or more occurrences.
  • +: Matches 1 or more occurrences.
  • ?: Matches 0 or 1 occurrence.
  • {n}: Matches exactly n occurrences.
  • {n,}: Matches n or more occurrences.
  • {n,m}: Matches between n and m occurrences.

  1. Anchors

  • ^: Matches the start of a string.
  • $: Matches the end of a string.

  1. Groups and Alternation

  • Parentheses (): Groups patterns together.
  • Pipe |: Acts as an OR operator.

Practical Examples

Example 1: Basic Matching

pattern = /hello/
puts pattern.match("hello world") # => #<MatchData "hello">
puts pattern.match("hi world")    # => nil

Explanation: The pattern /hello/ matches the substring "hello" in the string "hello world".

Example 2: Character Classes

pattern = /[aeiou]/
puts pattern.match("cat") # => #<MatchData "a">
puts pattern.match("sky") # => nil

Explanation: The pattern /[aeiou]/ matches any vowel in the string "cat".

Example 3: Quantifiers

pattern = /a{2,4}/
puts pattern.match("aaa")  # => #<MatchData "aaa">
puts pattern.match("aaaaa") # => #<MatchData "aaaa">

Explanation: The pattern /a{2,4}/ matches between 2 and 4 occurrences of the letter "a".

Example 4: Anchors

pattern = /^hello/
puts pattern.match("hello world") # => #<MatchData "hello">
puts pattern.match("world hello") # => nil

Explanation: The pattern /^hello/ matches "hello" only if it is at the start of the string.

Example 5: Groups and Alternation

pattern = /(cat|dog)/
puts pattern.match("I have a cat") # => #<MatchData "cat">
puts pattern.match("I have a dog") # => #<MatchData "dog">

Explanation: The pattern /(cat|dog)/ matches either "cat" or "dog".

Practical Exercises

Exercise 1: Validate Email Address

Write a regex pattern to validate an email address.

pattern = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/
puts pattern.match("[email protected]") # => #<MatchData "[email protected]">
puts pattern.match("invalid-email")       # => nil

Exercise 2: Extract Phone Numbers

Write a regex pattern to extract phone numbers from a string.

pattern = /\b\d{3}-\d{3}-\d{4}\b/
text = "Contact us at 123-456-7890 or 987-654-3210."
puts text.scan(pattern) # => ["123-456-7890", "987-654-3210"]

Exercise 3: Find All Words Starting with a Capital Letter

Write a regex pattern to find all words starting with a capital letter.

pattern = /\b[A-Z][a-z]*\b/
text = "Hello World! This is a Test."
puts text.scan(pattern) # => ["Hello", "World", "This", "Test"]

Common Mistakes and Tips

  • Escaping Metacharacters: Remember to escape metacharacters (e.g., .) if you want to match them literally.
  • Greedy vs. Non-Greedy Matching: By default, quantifiers are greedy. Use ? to make them non-greedy (e.g., .*?).
  • Testing Patterns: Use tools like Rubular (http://rubular.com/) to test and debug your regex patterns.

Conclusion

Regular expressions are a versatile tool for text processing in Ruby. By understanding the basic syntax, character classes, quantifiers, anchors, and groups, you can create powerful patterns to match and manipulate text. Practice with the provided exercises to reinforce your understanding and become proficient in using regular expressions.

© Copyright 2024. All rights reserved