|
|
Regular expressions (regexp) in Ruby provide powerful
pattern matching and text manipulation.
In Ruby, regexp is implemented using the |
|
Create a regular expression |
|
Ruby provides several ways to create regular
expressions:
Using |
|
Using |
|
Using |
|
Check if a string matches a pattern |
|
The most common use of regexp is to check if a string
matches a specific pattern. |
|
The |
|
Using |
|
Using |
|
Capture matching data |
|
It’s also useful to capture just the data that’s matching your pattern. For example, in a text blurb, you might want to extract just the dates. |
|
To help with that when a regex matches a string,
Ruby creates a |
|
|
|
Common Use Cases |
|
Validate an email: |
|
String substitution: |
|
Parse a URL: |
|
Notes & Reference |
|
|