Regex Fundamentals: Python re Module Basics Quiz Quiz

Challenge your understanding of regular expressions using the Python re module. Covering essential syntax, common functions, and practical matching scenarios, this quiz is designed for programmers looking to solidify their grasp of Python regex fundamentals.

  1. Pattern Matching Function

    Which function from the Python re module will return a Match object if the pattern '^abc' occurs at the beginning of the string 'abcdef'?

    1. re.match
    2. re.find
    3. re.contains
    4. re.scan

    Explanation: The re.match function checks for a match only at the beginning of the string, which is appropriate for the pattern '^abc' on 'abcdef'. re.find is not a function in the re module—re.findall is. re.contains and re.scan are also not valid Python re functions. Choosing the correct function ensures the pattern is evaluated at the intended position.

  2. Regex Special Characters

    When using the pattern 'd+' with re.search on the string 'Order123', what does the pattern 'd+' represent?

    1. One or more digits
    2. Zero or more letters
    3. A single non-digit character
    4. Exactly three digits

    Explanation: In regex, 'd' stands for any digit, and '+' means one or more occurrences, so 'd+' matches one or more digits in the target string. The pattern does not match letters or specifically three digits. 'D' (uppercase) would match non-digit characters. Thus, only 'one or more digits' accurately describes the pattern.

  3. Searching for Multiple Occurrences

    Given the string 'cat bat rat', which function would you use to extract all three animal words using the pattern 'w{3}'?

    1. re.findall
    2. re.match
    3. re.equal
    4. re.slice

    Explanation: The re.findall function returns all non-overlapping matches of the pattern in the string, making it suitable for extracting every three-letter word. re.match only checks the start of the string, not all occurrences. re.equal and re.slice are not valid methods of the re module, making them incorrect choices.

  4. Regex Flags

    If you want to search for the pattern 'apple' regardless of letter case within a string, which flag should be passed to the re functions?

    1. re.IGNORECASE
    2. re.MULTILINE
    3. re.DOTALL
    4. re.ESCAPE

    Explanation: The re.IGNORECASE flag allows case-insensitive matching, so 'apple', 'Apple', or 'APPLE' would all be found. re.MULTILINE changes how '^' and '$' tokens behave with multi-line strings. re.DOTALL allows the dot '.' to match newline characters. re.ESCAPE is not a standard flag in Python's re module.

  5. Regex Character Classes

    Which regex pattern will match any vowel (a, e, i, o, u) in a case-insensitive way when used with the appropriate flag?

    1. [aeiou]
    2. [Aeiou]
    3. [aeiou]{1,3}
    4. [aeiouAEIOU0-9]

    Explanation: [aeiou] matches any lowercase vowel, and when used with the case-insensitive flag, it will also match uppercase vowels. [Aeiou] only covers some uppercase and lowercase vowels. [aeiou]{1,3} is for one to three consecutive vowels, not just any vowel. [aeiouAEIOU0-9] unnecessarily includes digits, which is not required.