PYTHON Advanced Regex MCQs

  1. Which of the following enables non capturing groups?
    a) (?: )
    b) ()
    c) []
    d) {}
    View Answer

    Correct answer: A — (?: )

  2. What will be printed?
    1 python
    2 import re
    3 print(re.search("(?:Edu)inq","Eduinq").group())
    a) Eduinq
    b) Edu
    c) inq
    d) None
    View Answer

    Correct answer: A — Eduinq

  3. Which of the following enables named groups?
    a) (?P<name>pattern)
    b) (name:pattern)
    c) {name}pattern
    d) None
    View Answer

    Correct answer: A — (?P<name>pattern)

  4. What is the output of below code?
    1 python
    2 import re
    3 m = re.search("(?P<grp>Edu)inq","Eduinq")
    4 print(m.group("grp"))
    a) Edu
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  5. Which of the following enables lookahead assertion?
    a) (?=pattern)
    b) (?!pattern)
    c) (?<=pattern)
    d) (?<!pattern)
    View Answer

    Correct answer: A — (?=pattern)

  6. What will be printed?
    1 python
    2 import re
    3 print(re.search("Edu(?=inq)","Eduinq").group())
    a) Edu
    b) Eduinq
    c) inq
    d) None
    View Answer

    Correct answer: A — Edu

  7. Which of the following enables negative lookahead?
    a) (?!pattern)
    b) (?=pattern)
    c) (?<=pattern)
    d) (?<!pattern)
    View Answer

    Correct answer: A — (?!pattern)

  8. What is the output of print(re.search("Edu(?!abc)","Eduinq").group())?
    a) Edu
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  9. Which of the following enables lookbehind assertion?
    a) (?<=pattern)
    b) (?<!pattern)
    c) (?=pattern)
    d) (?!pattern)
    View Answer

    Correct answer: A — (?<=pattern)

  10. What will be printed?
    1 python
    2 import re
    3 print(re.search("(?<=Edu)inq","Eduinq").group())
    a) inq
    b) Eduinq
    c) Edu
    d) None
    View Answer

    Correct answer: A — inq

  11. Which of the following enables negative lookbehind?
    a) (?<!pattern)
    b) (?<=pattern)
    c) (?=pattern)
    d) (?!pattern)
    View Answer

    Correct answer: A — (?<!pattern)

  12. What is the output of print(re.search("(?<!Python)Edu","Eduinq").group())?
    a) Edu
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  13. Which of the following enables conditional regex?
    a) (?(id)yes|no)
    b) (?:condition)
    c) (?=condition)
    d) None
    View Answer

    Correct answer: A — (?(id)yes|no)

  14. What will be printed?
    1 python
    2 import re
    3 print(re.search("(?(1)Edu|inq)","inq"))
    a) inq
    b) Edu
    c) Error
    d) None
    View Answer

    Correct answer: A — inq

  15. Which of the following enables atomic grouping?
    a) (?>pattern)
    b) (?:pattern)
    c) (?=pattern)
    d) None
    View Answer

    Correct answer: A — (?>pattern)

  16. What is the output of print(re.search("(?>Edu)inq","Eduinq").group())?
    a) Eduinq
    b) Edu
    c) inq
    d) None
    View Answer

    Correct answer: A — Eduinq

  17. Which of the following enables inline flags?
    a) (?i)
    b) (?m)
    c) (?s)
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  18. What will be printed?
    1 python
    2 import re
    3 print(re.search("(?i)edu","EDUinq").group())
    a) EDU
    b) Edu
    c) edu
    d) None
    View Answer

    Correct answer: A — EDU

  19. Which of the following enables verbose regex?
    a) re.VERBOSE
    b) re.X
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  20. What is the output of below code?
    1 python
    2 import re
    3 pattern = re.compile(r"""
    4 Edu # match Edu
    5 inq # match inq
    6 """,re.VERBOSE)
    7 print(pattern.match("Eduinq").group())
    a) Eduinq
    b) Edu
    c) inq
    d) None
    View Answer

    Correct answer: A — Eduinq

  21. Which of the following enables inline comments in regex?
    a) re.VERBOSE
    b) re.COMMENT
    c) # inside pattern
    d) None
    View Answer

    Correct answer: A — re.VERBOSE

  22. What will be printed?
    1 python
    2 import re
    3 pattern = re.compile(r"Edu #comment",re.VERBOSE)
    4 print(pattern.match("Edu").group())
    a) Edu
    b) Edu #comment
    c) Error
    d) None
    View Answer

    Correct answer: A — Edu

  23. Which of the following enables backreference by number?
    a) \1
    b) \2
    c) \3
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  24. What is the output of below code?
    1 python
    2 import re
    3 m = re.search(r"(Edu)\1","EduEdu")
    4 print(m.group())
    a) EduEdu
    b) Edu
    c) Error
    d) None
    View Answer

    Correct answer: A — EduEdu

  25. Which of the following enables backreference by name?
    a) (?P=name)
    b) \name
    c) {name}
    d) None
    View Answer

    Correct answer: A — (?P=name)

  26. What will be printed?
    1 python
    2 import re
    3 m = re.search(r"(?P<grp>Edu)(?P=grp)","EduEdu")
    4 print(m.group())
    a) EduEdu
    b) Edu
    c) Error
    d) None
    View Answer

    Correct answer: A — EduEdu

  27. Which of the following enables possessive quantifiers (not backtracking)?
    a) *+
    b) ++
    c) ?+
    d) All of the above
    View Answer

    Correct answer: D — All of the above

  28. What is the output of print(re.search("Edu*+","Eduuuuu").group())?
    a) Eduuuuu
    b) Edu
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduuuuu

  29. Which of the following enables inline flag for multiline?
    a) (?m)
    b) (?i)
    c) (?s)
    d) None
    View Answer

    Correct answer: A — (?m)

  30. What will be printed?
    1 python
    2 import re
    3 print(re.search("(?m)^inq","Edu\ninq").group())
    a) inq
    b) Edu
    c) Error
    d) None
    View Answer

    Correct answer: A — inq

  31. Which of the following enables inline flag for dotall?
    a) (?s)
    b) (?m)
    c) (?i)
    d) None
    View Answer

    Correct answer: A — (?s)

  32. What is the output of print(re.search("(?s).+","Edu\ninq").group())?
    a) Edu\ninq
    b) Edu
    c) inq
    d) None
    View Answer

    Correct answer: A — Edu\ninq

  33. Which of the following enables inline flag for case insensitive?
    a) (?i)
    b) (?m)
    c) (?s)
    d) None
    View Answer

    Correct answer: A — (?i)

  34. What will be printed?
    1 python
    2 import re
    3 print(re.search("(?i)edu","EDUinq").group())
    a) EDU
    b) Edu
    c) edu
    d) None
    View Answer

    Correct answer: A — EDU

  35. Which of the following enables inline flag for locale dependent?
    a) (?L)
    b) (?i)
    c) (?m)
    d) None
    View Answer

    Correct answer: A — (?L)

  36. What is the output of print(re.search("(?L)\w+","Eduinq").group())?
    a) Eduinq
    b) Edu
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq

  37. Which of the following enables inline flag for unicode matching?
    a) (?u)
    b) (?U)
    c) (?n)
    d) None
    View Answer

    Correct answer: A — (?u)

  38. What will be printed?
    1 python
    2 import re
    3 print(re.search("(?u)\w+","Eduinq").group())
    a) Eduinq
    b) Edu
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq

  39. Which of the following enables inline flag for default greedy quantifiers?
    a) (?U)
    b) (?u)
    c) (?i)
    d) None
    View Answer

    Correct answer: A — (?U)

  40. What is the output of print(re.search("(?U).+?","Eduinq").group())?
    a) E
    b) Eduinq
    c) Error
    d) None
    View Answer

    Correct answer: A — E

  41. Which of the following enables atomic grouping in regex?
    a) (?>pattern)
    b) (?:pattern)
    c) (?=pattern)
    d) None
    View Answer

    Correct answer: A — (?>pattern)

  42. What will be printed?
    1 python
    2 import re
    3 print(re.search("(?>Edu)inq","Eduinq").group())
    a) Eduinq
    b) Edu
    c) inq
    d) None
    View Answer

    Correct answer: A — Eduinq

  43. Which of the following enables conditional matching with named group?
    a) (?(grp)yes|no)
    b) (?P=grp)
    c) (?=grp)
    d) None
    View Answer

    Correct answer: A — (?(grp)yes|no)

  44. What is the output of below code?
    1 python
    2 import re
    3 m = re.search(r"(?P<grp>Edu)?(?(grp)inq|Python)","Eduinq")
    4 print(m.group())
    a) Eduinq
    b) Python
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq

  45. Which of the following enables recursive regex?
    a) (?R)
    b) (?0)
    c) Both A and B
    d) None
    View Answer

    Correct answer: C — Both A and B

  46. What will be printed?
    1 python
    2 import re
    3 pattern = re.compile(r"(Edu)(?R)?")
    4 print(pattern.match("EduEdu").group())
    a) EduEdu
    b) Edu
    c) Error
    d) None
    View Answer

    Correct answer: A — EduEdu

  47. Which of the following enables balancing groups in regex?
    a) (?<name1-name2>pattern)
    b) (?P<name>pattern)
    c) (?:pattern)
    d) None
    View Answer

    Correct answer: A — (?<name1-name2>pattern)

  48. What is the output of print(re.search("(?<open-close>Edu)","Edu").group())?
    a) Edu
    b) Error
    c) None
    d) open
    View Answer

    Correct answer: A — Edu

  49. Which of the following enables inline flag for ignore pattern whitespace?
    a) (?x)
    b) (?i)
    c) (?m)
    d) None
    View Answer

    Correct answer: A — (?x)

  50. What will be printed?
    1 python
    2 import re
    3 pattern = re.compile(r"Edu inq",re.X)
    4 print(pattern.match("Eduinq").group())
    a) Eduinq
    b) Edu inq
    c) Error
    d) None
    View Answer

    Correct answer: A — Eduinq

Quick Links to Explore