0
0
Rubyprogramming~5 mins

Regex literal syntax (/pattern/) in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a regex literal in Ruby?
A regex literal in Ruby is a pattern written between slashes, like /pattern/, used to match text.
Click to reveal answer
beginner
How do you write a regex literal to match the word 'cat' exactly?
You write /cat/. This matches the exact sequence of characters 'cat' anywhere in the text.
Click to reveal answer
intermediate
What does the regex literal /\d+/ match?
It matches one or more digits in a row. \d means any digit, and + means one or more times.
Click to reveal answer
intermediate
How do you include options like case-insensitive matching in a regex literal?
Add a letter after the closing slash. For example, /cat/i matches 'cat', 'Cat', or 'CAT'.
Click to reveal answer
intermediate
How do you write a regex literal to match a string that starts with 'hello'?
Use the caret symbol ^ at the start: /^hello/. This matches 'hello' only at the beginning of the text.
Click to reveal answer
What does the regex literal /ruby/ match?
AThe exact sequence 'ruby' anywhere in the text
BOnly the word 'Ruby' with capital R
CAny word ending with 'ruby'
DOnly the start of a string
How do you write a regex literal to match one or more digits?
A/\d+/
B/\w+/
C/\s+/
D/\D+/
What does the i after a regex literal do? For example, /cat/i
AMatches only at the start of the string
BMakes the regex match whitespace
CMatches only digits
DMakes the match case-insensitive
Which regex literal matches a string that starts with 'hello'?
A/hello$/
B/^hello/
C/hello/
D/^hello$/
How do you write a regex literal to match a newline character?
A/\s/
B/\t/
C/\n/
D/\r/
Explain how to write a regex literal in Ruby and how to add options like case-insensitivity.
Think about the slashes and letters after them.
You got /4 concepts.
    Describe how anchors like ^ and $ work inside regex literals.
    Anchors fix where the match happens.
    You got /3 concepts.