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?"