Regular Expressions — the re Module — Concept Check
Questions on Regular Expressions — the re Module. search finds, findall collects, sub replaces, groups extract — with raw strings, character classes and quantifiers making Python a text-processing power tool.
Questions in this quiz
8 questions on Regular Expressions — the re Module — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses re.findall to extract user IDs from a log file. The pattern is written without groups. What will the output be?
- AA list of groups as tuples
- BAn empty list
- CA list of match objects
- DA list of entire matches as strings
2.A script replaces phone numbers with 'XXXXXXXXXX' using re.sub. The pattern is r'\d{10}'. What happens if the text contains '9876543210'?
- AAn error is thrown
- BThe number is replaced with 'XXXXXXXXXX'
- CThe number remains unchanged
- DOnly the first digit is replaced
3.A regex pattern is compiled with re.compile for matching Indian mobile numbers. Why is this beneficial in a loop?
- AIt improves performance by reusing the pattern
- BIt allows for lazy evaluation
- CIt automatically handles group extraction
- DIt ensures thread safety
4.A developer mistakenly believes re.match and re.search behave identically. What is a key difference?
- Are.match returns all matches
- Bre.search requires the pattern to be compiled
- Cre.match checks only at the start of the string
- Dre.search is faster than re.match
5.A regex pattern r'<.*>' is used to extract tags from HTML. What is a likely outcome?
- AIt captures only the first tag
- BIt raises a syntax error
- CIt captures the entire string between the first and last tags
- DIt captures each tag separately
6.When using re.sub with a pattern containing groups, what can backreferences do?
- AMatch the entire input string
- BRepeat the matched string
- CChange the regex pattern
- DRefer to captured groups in the replacement string
7.A developer uses re.findall with a pattern containing groups. What is returned?
- AA single match object
- BA list of match objects
- CA list of tuples containing group matches
- DA list of entire matches as strings
8.A regex pattern r'\bjava\b' is used in re.sub to replace 'java' with 'python'. What is the result for 'java javascript java'?
- A'python javascript java'
- B'python python python'
- C'java javascript python'
- D'python javascript python'