RegExp
Published Jul 30, 2021Updated Sep 9, 2021
Contribute to Docs
In Ruby, Regular Expressions (shortened as RegExp), are used to describe and match patterns in strings. This functionality is housed in the Regexp
class.
Syntax
Regular expressions can be created in three ways:
# With / /regexp1 = /code/# With %r{ }regexp2 = %r{code}# With Regexp.new()regexp3 = Regexp.new("code")
Finding Patterns in Strings
To see if a given string matches a regular expression, use .match()
.
puts regexp1.match("codecademy") # Output: code
To test if there even was a match (true
or false
), use .match?()
:
puts regexp1.match?("codecademy") # Output: true
All contributors
- Anonymous contributorAnonymous contributor3077 contributions
- christian.dinh
- Anonymous contributor
- BrandonDusch
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.