What does the regular expression pattern `^\d{3}-\d{4}$` match?
Answer: B
The `^` anchor asserts the start of the string and `$` asserts the end. `\d{3}` matches exactly three digits, `-` matches a literal hyphen, and `\d{4}` matches exactly four digits. Together, the pattern matches a complete string of the form `123-4567` and nothing else.
Q.2Medium
Which of the following `re` module functions returns all non-overlapping matches of a pattern in a string as a list of strings?
Answer: C
`re.findall()` scans the string from left to right and returns all non-overlapping matches as a list. `re.match()` only checks at the beginning, `re.search()` returns only the first match object, and `re.fullmatch()` requires the entire string to match.
Q.3Medium
What is the output of the following code?
```python
import re
result = re.sub(r'\s+', '-', 'Hello World Python')
print(result)
```
Answer: A
`re.sub(r'\s+', '-', ...)` replaces every sequence of one or more whitespace characters with a single hyphen. `'Hello World Python'` has two groups of spaces, so both are replaced by `-`, giving `Hello-World-Python`.
Q.4Medium
What does the `re.IGNORECASE` (or `re.I`) flag do when used in a regular expression?
Answer: B
`re.IGNORECASE` makes the regex engine perform case-insensitive matching, so patterns like `[a-z]` will also match uppercase letters. The flag that makes `.` match newlines is `re.DOTALL`, and `re.VERBOSE` allows whitespace/comments in patterns.
Q.5Medium
What is the difference between `re.match()` and `re.search()` in Python?
Answer: B
`re.match()` only attempts to match the pattern at the start of the string (position 0), while `re.search()` scans through the entire string looking for any location where the pattern matches. Both return a match object on success and `None` on failure.
Advertisement
Q.6Medium
What will the following code print?
```python
import re
pattern = r'(\w+)@(\w+)\.com'
m = re.search(pattern, 'Contact: user@example.com today')
if m:
print(m.group(1), m.group(2))
```
Answer: B
The pattern has two capturing groups: `(\w+)` before `@` and `(\w+)` before `.com`. `m.group(1)` returns the first captured group `user` and `m.group(2)` returns the second captured group `example`. So the output is `user example`.
Q.7Medium
Which regular expression pattern correctly matches a string that contains ONLY alphabetic characters (no digits, spaces, or special characters)?
Answer: A
`^[a-zA-Z]+$` uses `+` to require at least one character and `[a-zA-Z]` to allow only letters. Option B uses `*` which also matches an empty string. Option C (`\w`) also matches digits and underscores. Option D only matches lowercase letters.
Q.8Medium
What does the `?` quantifier mean when used in a regular expression like `colou?r`?
Answer: C
The `?` quantifier makes the preceding element optional, matching it zero or one time. So `colou?r` matches both `color` (without `u`) and `colour` (with `u`). It does not mean zero-or-more (`*`) or one-or-more (`+`).
Q.9Medium
What will the following code output?
```python
import re
text = 'cat bat sat'
result = re.findall(r'[cbr]at', text)
print(result)
```
Answer: B
The character class `[cbr]` matches only `c`, `b`, or `r`. So `cat` (matches `c`) and `bat` (matches `b`) are found. `sat` does not match because `s` is not in `[cbr]`. The result is `['cat', 'bat']`.
Q.10Medium
What is the purpose of using a raw string (prefix `r`) for regular expression patterns in Python, such as `r'\d+'`?
Answer: B
In a regular Python string, `\d` would be interpreted as an escape sequence (which is just `d` since `\d` is not a recognised Python escape). Using a raw string `r'\d+'` ensures the backslash is passed literally to the regex engine, which then correctly interprets `\d` as the digit shorthand. This avoids the need to double-escape patterns like `'\\d+'`.