📖 Scenario: You work in a small company that processes customer feedback. The feedback contains many sentences, and you want to find specific words or patterns inside these sentences to analyze customer sentiment.
🎯 Goal: You will create a MATLAB script that uses regular expressions to find all words starting with the letter 'a' in a list of customer feedback sentences.
📋 What You'll Learn
Create a cell array of strings called feedback with exact sentences.
Create a pattern string variable called pattern to find words starting with 'a'.
Use the regexp function with the 'match' option to find all matching words.
Print the matched words using disp.
💡 Why This Matters
🌍 Real World
Regular expressions help find patterns in text data like customer feedback, emails, or logs.
💼 Career
Knowing how to use regular expressions in MATLAB is useful for data analysis, text processing, and automating report generation.
Progress0 / 4 steps
1
Create the feedback data
Create a cell array called feedback with these exact sentences: 'I absolutely love this product', 'Amazing service and friendly staff', 'The ambiance was average', 'Affordable prices and good quality'.
MATLAB
Hint
Use curly braces {} to create a cell array of strings in MATLAB.
2
Define the regular expression pattern
Create a variable called pattern and set it to the regular expression string '\<[aA]\w*\>' to find words starting with the letter 'a'.
MATLAB
Hint
The pattern '\<[aA]\w*\>' matches words starting with 'a'. Use double backslashes in MATLAB strings.
3
Find all matching words using regexp
Use the regexp function with variables feedback and pattern, and the option 'match' to find all words starting with 'a'. Store the result in a variable called matches.
MATLAB
Hint
Use regexp with the 'match' option to get matching words as cell arrays.
4
Display the matched words
Use disp to print the variable matches to show all words starting with 'a' found in the feedback.
MATLAB
Hint
Use disp(matches) to print the cell array of matches.
Practice
(1/5)
1. What does the MATLAB function regexp do?
easy
A. Searches for text patterns in strings
B. Replaces text in strings
C. Sorts arrays numerically
D. Calculates the length of a string
Solution
Step 1: Understand the purpose of regexp
The regexp function in MATLAB is used to find matches of a pattern within text.
Step 2: Compare with other functions
regexprep replaces text, but regexp only searches for patterns.
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.