Advanced Regex MCQ 15 Tricky Questions
Time: 25-35 mins Intermediate/Advanced

Tricky Python Regular Expressions MCQ Challenge

Test your mastery of Python regular expressions with 15 challenging multiple choice questions. Covers greedy vs lazy matching, lookaheads/lookbehinds, groups and backreferences, flags, performance considerations, and tricky edge cases that often trip up developers.

Greedy vs Lazy

* vs *?, + vs +?

Lookaheads

(?=) and (?!)

Groups

Capturing, non-capturing

Flags

re.IGNORECASE, re.DOTALL

Mastering Python Regular Expressions: Advanced Concepts and Tricky Behaviors

Python regular expressions (regex) are a powerful tool for text processing, but they hide numerous subtleties that can lead to unexpected matches, performance issues, or unreadable patterns. This MCQ test focuses on the tricky aspects of regex—greedy vs lazy quantifiers, lookaheads and lookbehinds, group capturing vs non-capturing, backreferences, flag behaviors, and common pitfalls that trap even experienced developers.

import re

# Basic pattern matching
pattern = r'\d{3}-\d{2}-\d{4}' # SSN pattern
text = 'My SSN is 123-45-6789'
match = re.search(pattern, text)

# Using groups
pattern = r'(\d{3})-(\d{2})-(\d{4})'
match = re.search(pattern, text)
if match:
  print(match.group(1)) # 123
  print(match.groups()) # ('123', '45', '6789')

Advanced Regex Concepts Covered

  • Greedy vs Lazy

    *, +, ?, {m,n} vs *?, +?, ??, {m,n}?

  • Lookaheads & Lookbehinds

    (?=), (?!), (?<=), (?

  • Groups & Backreferences

    Capturing ( ), non-capturing (?: ), named (?P<name> ), \1, \g<name>

  • Flags & Modifiers

    re.IGNORECASE, re.MULTILINE, re.DOTALL, re.VERBOSE

  • Performance

    Catastrophic backtracking, optimization, compiled patterns

  • Common Pitfalls

    Dot doesn't match newline, ^/$ anchors, raw strings

Why These Tricky Regex Questions Matter

Regular expressions are used for validation, parsing, text extraction, and transformation in countless applications. However, complex regex patterns can become unreadable, perform poorly with catastrophic backtracking, or have subtle matching behaviors that lead to bugs. Understanding greedy vs lazy quantifiers, proper use of lookaheads for complex conditions, group capturing strategies, and performance optimization is crucial for writing robust text processing code. These questions test attention to regex subtleties that can lead to security vulnerabilities, data corruption, or performance degradation.

Key Regex Insight

Always use raw strings (r'pattern') for regex patterns to avoid double backslash escaping. Understand that . (dot) doesn't match newlines unless re.DOTALL flag is used. Greedy quantifiers (*, +) match as much as possible; lazy ones (*?, +?) match as little as possible. Compile frequently used patterns with re.compile() for better performance.

Pro Tip: Use re.VERBOSE flag for complex patterns—it allows whitespace and comments for readability. Test regex thoroughly with edge cases. Beware of catastrophic backtracking with nested quantifiers like (a+)+. Consider using regex debuggers or online testers like regex101.com. Sometimes simple string methods (str.find(), str.replace()) are better than regex.

Common Regex Patterns and Pitfalls

Backtracking

Catastrophic backtracking from nested quantifiers can freeze your app.

Dot & Newlines

. doesn't match \n unless re.DOTALL flag is used.

Anchors

^ and $ behave differently with re.MULTILINE flag.

Greedy vs Lazy Quantifiers Examples

Text: "foo <bar> <baz>"
Greedy pattern: r'<.*>' matches entire string "<bar> <baz>"
Lazy pattern: r'<.*?>' matches first tag only "<bar>"
Greedy quantifiers match as much as possible; lazy ones match as little as possible.
Lookahead example: r'\w+(?=\.)' matches word before a period without consuming the period.
Lookbehind example: r'(?<=\$)\d+' matches digits preceded by $ without consuming $.
Lookaheads/lookbehinds are zero-width assertions—they don't consume characters.

When to Use Regex vs String Methods

Use regular expressions when:

  • You need pattern matching (not just fixed strings)
  • You need to extract multiple parts of text (groups)
  • You need to validate complex formats (emails, URLs, phone numbers)
  • You need to find/replace with patterns

Use string methods when:

  • You're working with fixed strings (str.find(), str.replace())
  • You need simple prefix/suffix checks (str.startswith(), str.endswith())
  • Readability is more important than power
  • Performance is critical (string methods are faster)
Debugging Tip: If your regex isn't matching as expected, try: 1) Check if you're using raw strings (r'pattern'), 2) Test with re.DEBUG flag, 3) Use an online regex tester, 4) Break complex patterns into smaller parts, 5) Consider if you really need regex or if string methods would work.