0
0
MATLABdata~20 mins

Regular expressions in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Master in MATLAB
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of regexpi with case-insensitive matching
What is the output of the following MATLAB code?
str = 'Matlab is fun';
expr = 'MAT';
idx = regexpi(str, expr);
disp(idx);
MATLAB
str = 'Matlab is fun';
expr = 'MAT';
idx = regexpi(str, expr);
disp(idx);
A[]
B[1]
C[4]
D[7]
Attempts:
2 left
💡 Hint
regexpi is case-insensitive and returns start indices of matches.
Predict Output
intermediate
2:00remaining
Using regexp to extract tokens
What does the following MATLAB code output?
str = 'abc123def456';
tokens = regexp(str, '\\d+', 'match');
disp(tokens);
MATLAB
str = 'abc123def456';
tokens = regexp(str, '\d+', 'match');
disp(tokens);
A{'abc', 'def'}
B{'1', '2', '3', '4', '5', '6'}
C{'123', '456'}
D[]
Attempts:
2 left
💡 Hint
The pattern '\d+' matches one or more digits.
Predict Output
advanced
2:00remaining
Output of regexprep with backreference
What is the output of this MATLAB code?
str = 'cat bat rat';
newStr = regexprep(str, '(\w+)at', '$1ot');
disp(newStr);
MATLAB
str = 'cat bat rat';
newStr = regexprep(str, '(\w+)at', '$1ot');
disp(newStr);
A'cat bot rat'
B'cat bat rat'
C'cot bat rat'
D'cot bot rot'
Attempts:
2 left
💡 Hint
Backreference $1 refers to the first captured group.
Predict Output
advanced
2:00remaining
Error type from invalid regex pattern
What error does this MATLAB code produce?
str = 'hello';
expr = '(hello';
matches = regexp(str, expr);
MATLAB
str = 'hello';
expr = '(hello';
matches = regexp(str, expr);
AError: Unbalanced or unexpected parenthesis.
BError: Undefined function or variable 'hello'.
CError: Invalid character in expression.
DNo error, matches = [1].
Attempts:
2 left
💡 Hint
Check if the parentheses in the pattern are balanced.
🧠 Conceptual
expert
3:00remaining
Number of matches found by regexpi with overlapping patterns
Consider the MATLAB code below:
str = 'aaaa';
expr = 'aa';
matches = regexpi(str, expr);
numMatches = length(matches);

What is the value of numMatches after running this code?
MATLAB
str = 'aaaa';
expr = 'aa';
matches = regexpi(str, expr);
numMatches = length(matches);
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
regexpi does not find overlapping matches by default.