Regex literal syntax (/pattern/) in Ruby - Time & Space Complexity
We want to understand how the time it takes to use a regex pattern changes as the input grows.
How does matching a pattern scale when the text gets longer?
Analyze the time complexity of the following code snippet.
text = "hello world" * n
pattern = /world/
result = text.match(pattern)
This code checks if the word "world" appears in a long repeated text string.
Look at what repeats when matching the pattern.
- Primary operation: The regex engine scans the text characters one by one to find the pattern.
- How many times: Up to once per character in the text, depending on where the pattern is found.
As the text gets longer, the regex engine checks more characters.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 110 checks |
| 100 | About 1100 checks |
| 1000 | About 11000 checks |
Pattern observation: The work grows roughly in direct proportion to the text length.
Time Complexity: O(n)
This means the time to find the pattern grows linearly as the text gets longer.
[X] Wrong: "Regex matching always takes the same time no matter the text size."
[OK] Correct: The regex engine usually checks each character, so longer text means more work.
Understanding how regex matching scales helps you write efficient code and explain your choices clearly.
"What if the pattern was more complex with repetitions or optional parts? How would that affect the time complexity?"
Practice
cat?Solution
Step 1: Understand regex literal syntax
In Ruby, regex literals are written between slashes, like/pattern/.Step 2: Identify the correct pattern for 'cat'
The pattern to match the word 'cat' is simply/cat/.Final Answer:
/cat/ -> Option DQuick Check:
Regex literal = /pattern/ [OK]
- Using quotes instead of slashes
- Calling a non-existent regex() function
- Confusing string and regex syntax
Dog case-insensitively in Ruby?Solution
Step 1: Identify the case-insensitive option
In Ruby regex, theioption after the slashes makes matching case-insensitive.Step 2: Check the options given
Only/Dog/iuses the correctiflag for case-insensitive matching.Final Answer:
/Dog/i -> Option CQuick Check:
Case-insensitive flag = i [OK]
- Using g or m flags which mean different things
- Omitting the i flag for case-insensitivity
- Placing flags inside the slashes
text = "Hello World" match = text =~ /world/i puts match
Solution
Step 1: Understand the =~ operator with regex
The operator=~returns the index of the first match or nil if no match.Step 2: Analyze the regex and string
The regex/world/imatches 'World' ignoring case. In "Hello World", 'World' starts at index 6 (0-based).Final Answer:
6 -> Option BQuick Check:
Case-insensitive match index = 6 [OK]
- Ignoring case-insensitive flag i
- Thinking output is true/false instead of index
- Assuming match starts at 0 always
pattern = /hello/i/
Solution
Step 1: Check regex literal syntax
A regex literal starts and ends with a single slash. Here, there is an extra slash at the end.Step 2: Identify the syntax error
The extra slash after/hello/iis invalid and causes a syntax error.Final Answer:
Extra slash at the end causes syntax error -> Option AQuick Check:
Regex literal must start and end with one slash pair [OK]
- Adding extra slash after regex
- Confusing regex with string quotes
- Thinking flags go inside slashes
a or A in a string using a regex literal. Which regex literal correctly matches this requirement?Solution
Step 1: Understand the requirement
We want words starting with 'a' or 'A'. Using case-insensitive flagisimplifies matching both.Step 2: Analyze options
/\b[a]\w*/i uses/\b[a]\w*/iwhich matches word boundary, then 'a' (case-insensitive), then zero or more word chars. This matches words starting with 'a' or 'A'.Final Answer:
/\b[a]\w*/i -> Option AQuick Check:
Use i flag to match both 'a' and 'A' easily [OK]
- Listing both 'a' and 'A' inside brackets without i flag
- Using wrong character classes
- Missing word boundary \b
Ready to prove it?
Pass the test and this pattern moves unknown
