0
0
MATLABdata~10 mins

Regular expressions in MATLAB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find matches of the pattern 'cat' in the text.

MATLAB
matches = regexp(text, '[1]');
Drag options to blanks, or click blank then click option'
A'dog'
B'cat'
C'bat'
D'rat'
Attempts:
3 left
💡 Hint
Common Mistakes
Using double quotes instead of single quotes for the pattern.
Using a pattern that does not exist in the text.
2fill in blank
medium

Complete the code to extract all words starting with 'a' or 'A' from the text.

MATLAB
words = regexp(text, '[1]', 'match');
Drag options to blanks, or click blank then click option'
A'\b[aA]\w*\b'
B'\d+'
C'cat'
D'\s'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using word boundaries, causing partial matches.
Using '\d+' which matches digits, not words.
3fill in blank
hard

Fix the error in the code to find the position of the first digit in the string.

MATLAB
pos = regexp(str, '[1]');
Drag options to blanks, or click blank then click option'
A'\D'
B'[a-zA-Z]'
C'\d'
D'\s'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '[a-zA-Z]' which matches letters, not digits.
Using '\D' which matches non-digit characters.
4fill in blank
hard

Fill both blanks to extract all email addresses from the text.

MATLAB
emails = regexp(text, '[1]', '[2]');
Drag options to blanks, or click blank then click option'
A'[\w\.]+@[\w\.]+\.[a-zA-Z]{2,}'
B'start'
C'match'
D'end'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' or 'end' instead of 'match' to get the actual emails.
Using an incomplete or incorrect email pattern.
5fill in blank
hard

Fill all three blanks to replace all whitespace sequences with a single dash '-' in the string.

MATLAB
newStr = regexprep(str, '[1]', '[2]', '[3]');
Drag options to blanks, or click blank then click option'
A'\s+'
B'-'
C'all'
D'once'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'once' which replaces only the first match.
Using incorrect pattern that does not match whitespace sequences.