Regular expressions in MATLAB - Time & Space Complexity
When using regular expressions in MATLAB, it is important to understand how the time to find matches grows as the input text gets longer.
We want to know how the work done by MATLAB changes when searching bigger strings.
Analyze the time complexity of the following code snippet.
text = repmat('abc123', 1, n);
pattern = '\d+'; % match one or more digits
matches = regexp(text, pattern, 'match');
This code searches a long string made by repeating 'abc123' n times, looking for digit sequences.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the input string character by character to find matches.
- How many times: The scan goes through the entire string of length proportional to 6*n.
As the input string gets longer, the time to search grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 60 characters scanned |
| 100 | About 600 characters scanned |
| 1000 | About 6000 characters scanned |
Pattern observation: Doubling the input roughly doubles the work done.
Time Complexity: O(n)
This means the time to find matches grows linearly with the length of the input string.
[X] Wrong: "Regular expressions always run instantly regardless of input size."
[OK] Correct: The search must check each character, so bigger inputs take more time.
Understanding how regular expression search time grows helps you write efficient code and explain performance clearly.
"What if we changed the pattern to a more complex one with nested groups? How would the time complexity change?"
Practice
regexp do?Solution
Step 1: Understand the purpose of regexp
Theregexpfunction in MATLAB is used to find matches of a pattern within text.Step 2: Compare with other functions
regexprepreplaces text, butregexponly searches for patterns.Final Answer:
Searches for text patterns in strings -> Option AQuick Check:
regexp = search function [OK]
- Confusing regexp with regexprep
- Thinking regexp modifies strings
- Mixing regexp with numeric functions
'abc123' using regexp?Solution
Step 1: Identify the pattern for digits
In regular expressions,\dmatches digits, and+means one or more.Step 2: Check the options
regexp('abc123', '\\d+') uses\\d+which correctly matches one or more digits. Others match word characters, spaces, or non-digits.Final Answer:
regexp('abc123', '\\d+') -> Option AQuick Check:
Digits = \d+ pattern [OK]
- Using \w+ which matches letters and digits
- Using \s+ which matches spaces
- Using \D+ which matches non-digits
str = 'cat bat rat';
matches = regexp(str, '\\b\w{3}\b', 'match');
disp(matches);Solution
Step 1: Understand the pattern '\\b\\w{3}\\b'
The pattern matches word boundaries\\baround exactly 3 word characters\\w{3}.Step 2: Apply pattern to string
The string has three 3-letter words: 'cat', 'bat', and 'rat'. All match the pattern.Final Answer:
['cat', 'bat', 'rat'] -> Option DQuick Check:
Three 3-letter words matched [OK]
- Ignoring word boundaries and matching partial words
- Expecting concatenated matches
- Confusing \w with digits only
str with '#' but does not work as expected:
str = 'Room 101'; newStr = regexprep(str, '\d', '#'); disp(newStr);What is the error and how to fix it?
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 BQuick Check:
regexprep replaces all matches by default [OK]
- Thinking \d replaces only one digit total
- Using \D which matches non-digits
- Confusing regexp with regexprep
{'abc123', 'def456', 'ghi789'}. How do you extract only the numeric parts from each string using MATLAB regular expressions?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
regexprepreplaces patterns. Using\\Dmatches non-digits, so replacing them with empty string keeps only digits.Final Answer:
Use regexprep(cellArray, '\\D', '') -> Option CQuick Check:
Remove non-digits with \D pattern [OK]
- Using regexp with 'match' returns cell arrays, not strings
- Replacing digits instead of non-digits
- Using \w+ which matches letters and digits
Ready to prove it?
Pass the test and this pattern moves unknown
