0
0
Rubyprogramming~10 mins

Regex literal syntax (/pattern/) in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Regex literal syntax (/pattern/)
Start
Write /pattern/
Ruby reads /pattern/
Create Regex object
Use Regex for matching
Return match result or nil
End
Ruby reads the pattern between slashes as a Regex object, then uses it to match strings, returning match info or nil.
Execution Sample
Ruby
text = "hello123"
pattern = /\d+/  # digits
match = text.match(pattern)
puts match[0] if match
This code finds the first group of digits in the text and prints it.
Execution Table
StepCode LineActionVariable StatesOutput
1text = "hello123"Assign string to texttext='hello123'
2pattern = /\d+/Create Regex object for digitspattern=/\d+/
3match = text.match(pattern)Match digits in textmatch=#<MatchData "123">
4puts match[0] if matchPrint matched digitsmatch=#<MatchData "123">123
5EndNo more codetext='hello123', pattern=/\d+/, match=#<MatchData "123">
💡 Reached end of code after printing matched digits
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
textnil"hello123""hello123""hello123""hello123""hello123"
patternnilnil/\d+//\d+//\d+//\d+/
matchnilnilnil#<MatchData "123">#<MatchData "123">#<MatchData "123">
Key Moments - 2 Insights
Why do we write the pattern between slashes (/pattern/) instead of quotes?
In Ruby, slashes create a Regex object directly, unlike quotes which create strings. See step 2 in execution_table where /\d+/ becomes a Regex.
What happens if the pattern does not match the text?
The match variable becomes nil, so the condition 'if match' prevents errors. This is shown in step 3 where match would be nil if no digits found.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of 'match'?
A"123"
Bnil
C#<MatchData "123">
D/\d+/
💡 Hint
Check the 'Variable States' column at step 3 in execution_table.
At which step does Ruby create the Regex object?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for when 'pattern' changes from nil to /\d+/ in variable_tracker and execution_table.
If the text was "hello" (no digits), what would 'match' be at step 3?
A#<MatchData "">
Bnil
C"hello"
D/\d+/
💡 Hint
Recall that match is nil if no pattern found, as explained in key_moments second question.
Concept Snapshot
Regex literal syntax in Ruby:
- Write pattern between slashes: /pattern/
- Creates a Regex object directly
- Use with String#match to find matches
- Returns MatchData or nil
- Example: /\d+/ matches digits
Full Transcript
This example shows how Ruby reads a regex literal written between slashes, like /\d+/, to create a Regex object. The code assigns a string to 'text', then creates a regex pattern to find digits. Using 'text.match(pattern)', Ruby searches for digits and returns a MatchData object if found. The program prints the matched digits '123'. If no match is found, 'match' would be nil, and the print is skipped. This step-by-step trace helps understand how regex literals work in Ruby.