Regular expressions are a powerful tool for searching and manipulating strings in many programming languages, including Ruby. They allow us to define patterns that can match specific parts of a string, making it easier to extract and manipulate data.
In Ruby, regular expressions are denoted by placing the pattern between forward slashes (/pattern/). For example, the regular expression /cat/ will match the word "cat" in a string. However, what if we want to match all occurrences of a pattern in a string? This is where the "global" flag comes into play.
To match all occurrences of a regular expression in Ruby, we need to add the "g" flag to our regular expression. This tells Ruby to find all matches in the string instead of just the first one. Let's look at an example:
```
string = "The cat sat on the mat"
matches = string.scan(/cat/g)
puts matches
```
The output of this code would be an array containing the string "cat" twice, as both occurrences of "cat" in the string have been matched.
Regular expressions also allow us to use special characters to define more complex patterns. For example, the dot (.) character represents any single character, and the asterisk (*) character denotes zero or more occurrences of the preceding character. Let's see how we can use these special characters to match all occurrences of a specific pattern.
```
string = "The car parked in the garage"
matches = string.scan(/c.r/g)
puts matches
```
In this example, the dot (.) character matches any single character, so the regular expression /c.r/ would match both "car" and "garage" in the string. The "g" flag ensures that all occurrences of this pattern are found and returned.
Regular expressions also allow us to use character classes to match a specific set of characters. For example, the character class [aeiou] would match any vowel, while [0-9] would match any digit. We can also use the caret (^) symbol to match any character that is not in a specific character class. Let's see how this works in action.
```
string = "I have 5 cats and 2 dogs"
matches = string.scan(/[aeiou]/g)
puts matches
```
In this example, the character class [aeiou] will match all vowels in the string, including the "a" in "cats" and the "o" in "dogs". The "g" flag ensures that all occurrences of this pattern are found and returned.
We can also use regular expressions to extract specific parts of a string. This is known as capturing groups and is denoted by placing parentheses around the part of the pattern we want to extract. Let's look at an example:
```
string = "My name is John and I am 25 years old"
matches = string.match(/My name is (\w+) and I am (\d+) years old/)
name = matches[1]
age = matches[2]
puts "Hello, my name is #{name} and I am #{age} years old."
```
In this example, we use capturing groups to extract the name and age from the string and then use string interpolation to output a personalized message. The parentheses around (\w+) and (\d+) capture these specific parts of the string and store them in the matches array. We can then access these values using the index number of the capturing group.
In summary, regular expressions are a powerful tool for matching and manipulating strings in Ruby. By adding the "g" flag, we can match all occurrences of a specific pattern in a string, making it easier to extract and manipulate data. With the addition of special characters and character classes, we can define more complex patterns to match specific parts of a string. And by using capturing groups, we can extract specific parts of a string for further manipulation. So next time you need to search or manipulate a string in Ruby, consider using regular expressions to make your life easier.