A. Pattern should be '\\d+' to replace all digits at once
B. No error; code works correctly
C. Pattern should be '\\D' to match digits
D. Use regexp instead of regexprep
Solution
Step 1: Analyze the regexprep usage
The pattern '\\d' matches any single digit, and regexprep replaces all occurrences by default.
Step 2: Check the output
Replacing each digit with '#' in 'Room 101' results in 'Room ###', which is expected.
Final Answer:
No error; code works correctly -> Option B
Quick Check:
regexprep replaces all matches by default [OK]
Hint: regexprep replaces all matches even with single-digit pattern [OK]
Common Mistakes:
Thinking \d replaces only one digit total
Using \D which matches non-digits
Confusing regexp with regexprep
5. You have a cell array of strings: {'abc123', 'def456', 'ghi789'}. How do you extract only the numeric parts from each string using MATLAB regular expressions?
hard
A. Use regexp(cellArray, '\\d+', 'match')
B. Use regexp(cellArray, '\\w+', 'match')
C. Use regexprep(cellArray, '\\D', '')
D. Use regexprep(cellArray, '\\d+', '')
Solution
Step 1: Understand the goal
We want to keep only digits, removing all non-digit characters.
Step 2: Choose the correct function and pattern
regexprep replaces patterns. Using \\D matches non-digits, so replacing them with empty string keeps only digits.
Final Answer:
Use regexprep(cellArray, '\\D', '') -> Option C
Quick Check:
Remove non-digits with \D pattern [OK]
Hint: Replace non-digits (\D) with empty string to keep digits [OK]
Common Mistakes:
Using regexp with 'match' returns cell arrays, not strings
Replacing digits instead of non-digits
Using \w+ which matches letters and digits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.