Regex - 5 mins read

Mastering Regular Expressions in Ruby

We’ll explore various aspects of regular expressions in Ruby

Saikat Kumar Dey

Technical Consultant

Overview

Regular expressions (regex) are powerful tools for pattern matching and text manipulation in programming languages like Ruby. They provide a concise and flexible way to search, extract, and replace text based on specific patterns. In this post, we’ll explore various aspects of regular expressions in Ruby, along with practical examples.

1. Matching Literal Characters:

Literal characters in regular expressions match exactly the same characters in the input string.

image1.webp

In this example, the pattern /hello/ matches the string “hello world” because it contains the literal character “hello”.

2. Character Classes:

Character classes allow you to match any one of a set of characters.

images2.webp

Here, the pattern [aeiou] matches any vowel in the string “hello”.

3. Quantifiers:

Quantifiers specify how many instances of the preceding character or group are required for a match.

image3.webp

The pattern \d+ matches one or more digits in the string “123”.

image4.webp

Here, this pattern matches exactly one digit in the string “123”.

4. Anchors:

Anchors are used to specify the position of a match in the input string.

0_53Rn9iU9pRwpJelE.webp

The pattern \Ahello matches “hello” only if it appears at the beginning of the string.

5. Alternation:

Alternation allows you to match one pattern or another.

image6.webp

This pattern matches either “cat” or “dog” in the string “I have a dog”.

6. Grouping:

Grouping allows you to apply quantifiers or alternations to multiple characters.

image7.webp

In this example, the pattern (\d+)-(\d+) matches and captures two sequences of digits separated by a hyphen in the string “123–456”.

7. Named Captures:

Named captures allow you to give names to captured groups for easier access.

image8.webp

This pattern matches and captures the year, month, and day in the string “2024–03–21” using named captures.

8. Lookahead and Lookbehind Assertions:

Lookahead and lookbehind assertions are used to assert whether a pattern is followed or preceded by another pattern, without including the pattern in the match.

image9.webp

These examples show positive and negative lookahead and lookbehind assertions.

9. Modifiers:

Modifiers change the behavior of the regular expression matching.

image10.webp

The i modifier makes the pattern case-insensitive, so it matches “hello” in any case.

Testing and Debugging:

image11.webp

  1. Backreferences: Backreferences allow you to refer to captured groups within the same regular expression.

image12.webp

The pattern \1 refers back to the first captured group, ensuring that the same word appears twice in the string.

##11. Non-Capturing Groups:

Non-capturing groups are like regular groups, but they do not capture the matched text.

image13.webp

In this example, the (?:bar) is a non-capturing group, matching “foo” followed by “bar” but only capturing “foo”.

12. Greedy vs. Lazy Quantifiers:

Greedy quantifiers match as much text as possible, while lazy quantifiers match as little text as possible.

greedy.webp

The first pattern matches the entire string “foo” and “bar”, while the second pattern matches “foo” and “bar” separately.

13. Atomic Groups:

Atomic groups are similar to non-capturing groups but prevent backtracking.

atomic.webp

14. Unicode Support:

Unicode support allows you to work with characters from any language.

unicode.webp

This pattern matches one or more Unicode letters, allowing for multilingual text processing.

Difference between Match and Scan Methods in Ruby Regular Expressions

In Ruby, both the match and scan methods are essential tools for working with regular expressions, but they serve different purposes:

Match Method (match):

The match method is used to find the first occurrence of a pattern in a string. It returns a MatchData object if a match is found or nil if no match is found. If the pattern contains capturing groups, you can access the captured substrings from the MatchData object. This method is typically used when you want to extract specific information or perform operations based on the first match in a string.

match.webp

In this example, the match method finds the first occurrence of the pattern (h\w+) in the string “hello world” and returns a MatchData object. We then access the first captured group using match_data[1], which outputs “hello”.

Scan Method (scan):

The scan method, on the other hand, is used to find all occurrences of a pattern in a string. It returns an array containing all matches found in the string. If the pattern contains capturing groups, scan returns an array of arrays, where each inner array represents the captured groups for each match. This method is typically used when you need to find multiple occurrences of a pattern in a string, such as when you’re extracting multiple pieces of information or analyzing text.

scan.webp

In this example, the scan method finds all word characters (\w+) in the string “hello world” and returns an array containing all matches found. The output is [“hello”, “world”], which represents the words found in the string.

In summary, use the match method when you need to find the first occurrence of a pattern and potentially extract specific information from it. Use the scan method when you need to find all occurrences of a pattern in a string, such as when you’re extracting multiple pieces of information or analyzing text.

Written by Saikat Kumar Dey

Other blogs

You may also like


Your one-stop shop for expert RoR services

oin 250+ companies achieving top-notch RoR development without increasing your workforce.