0
0
Rubyprogramming~10 mins

Common patterns and character classes in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Common patterns and character classes
Start
Define regex pattern
Use character classes
Match string with pattern?
NoNo match
Yes
Extract or use matched data
End
This flow shows how a regex pattern with character classes is defined and used to match strings, then extract or use the matched parts.
Execution Sample
Ruby
pattern = /[a-zA-Z]+/  # letters only
text = "Hello123"
match = text.match(pattern)
puts match[0] if match
This Ruby code finds the first sequence of letters in the text and prints it.
Execution Table
StepActionPatternInput StringMatch ResultOutput
1Define pattern/[a-zA-Z]+/"Hello123"N/AN/A
2Match pattern to string/[a-zA-Z]+/"Hello123""Hello"N/A
3Check if match existsN/AN/AMatch foundN/A
4Print matched substringN/AN/A"Hello"Hello
5EndN/AN/AN/AN/A
💡 Matching stops after first letter sequence found; rest of string ignored.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
patternundefined/[a-zA-Z]+//[a-zA-Z]+//[a-zA-Z]+//[a-zA-Z]+/
textundefined"Hello123""Hello123""Hello123""Hello123"
matchnilMatchData("Hello")MatchData("Hello")MatchData("Hello")MatchData("Hello")
outputundefinedundefinedundefined"Hello""Hello"
Key Moments - 3 Insights
Why does the pattern /[a-zA-Z]+/ only match 'Hello' and not the numbers?
Because the character class [a-zA-Z] matches only letters, and + means one or more letters in a row. Numbers are not letters, so matching stops before digits. See execution_table step 2.
What happens if the input string starts with numbers, like '123Hello'?
The pattern will not match at the start because it expects letters first. The match method finds the first substring matching the pattern anywhere, so it will match 'Hello' later. This is shown by the pattern behavior in step 2.
Why do we check if match exists before printing?
Because if no match is found, match will be nil and trying to access match[0] causes an error. Checking prevents errors. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what substring does the pattern match in "Hello123"?
A"123"
B"Hello123"
C"Hello"
D"" (empty string)
💡 Hint
Check the 'Match Result' column at step 2 in execution_table.
At which step do we confirm that a match was found before printing?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where match existence is checked.
If the input string was "123Hello", what would the match result be at step 2?
A"Hello"
Bnil (no match)
C"123"
D"123Hello"
💡 Hint
Remember the pattern matches letters only, so it skips initial digits and finds letters later.
Concept Snapshot
Ruby regex uses character classes like [a-zA-Z] to match letters.
+ means one or more repetitions.
.match returns first match or nil.
Always check if match exists before using it.
Character classes help target specific groups of characters.
Full Transcript
This example shows how Ruby uses regular expressions with character classes to find patterns in text. We define a pattern that matches one or more letters using [a-zA-Z]+. When we apply this pattern to the string "Hello123", it matches the first sequence of letters "Hello" and ignores the numbers. We check if a match was found before printing to avoid errors. This process helps extract specific parts of text based on character types.