Concept Flow - Common patterns and character classes
Start
↓
Define regex pattern
↓
Use character classes
↓
Match string with pattern?
No→No 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)
putsmatch[0] ifmatch
This Ruby code finds the first sequence of letters in the text and prints it.
Execution Table
Step
Action
Pattern
Input String
Match Result
Output
1
Define pattern
/[a-zA-Z]+/
"Hello123"
N/A
N/A
2
Match pattern to string
/[a-zA-Z]+/
"Hello123"
"Hello"
N/A
3
Check if match exists
N/A
N/A
Match found
N/A
4
Print matched substring
N/A
N/A
"Hello"
Hello
5
End
N/A
N/A
N/A
N/A
💡 Matching stops after first letter sequence found; rest of string ignored.
Variable Tracker
Variable
Start
After Step 2
After Step 3
After Step 4
Final
pattern
undefined
/[a-zA-Z]+/
/[a-zA-Z]+/
/[a-zA-Z]+/
/[a-zA-Z]+/
text
undefined
"Hello123"
"Hello123"
"Hello123"
"Hello123"
match
nil
MatchData("Hello")
MatchData("Hello")
MatchData("Hello")
MatchData("Hello")
output
undefined
undefined
undefined
"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.