What is the purpose of the <code>Regex</code> class in Kotlin?
The <code>Regex</code> class in Kotlin is used to work with regular expressions, allowing you to search, match, and manipulate text based on patterns.
Click to reveal answer
beginner
How do you create a Regex object to match the word "cat" in Kotlin?
You create it by calling Regex("cat"). This creates a pattern that looks for the exact sequence "cat" in text.
Click to reveal answer
beginner
What does the matches() function of a Regex object do?
The matches() function checks if the entire input string exactly matches the pattern defined by the Regex.
Click to reveal answer
intermediate
How can you find all occurrences of a pattern in a string using Regex?
Use the findAll() function on a Regex object. It returns a sequence of all matches found in the input string.
Click to reveal answer
intermediate
What is the difference between containsMatchIn() and matches() in Regex?
containsMatchIn() checks if the pattern appears anywhere inside the string, while matches() requires the whole string to match the pattern exactly.
Click to reveal answer
Which Kotlin class is used to work with regular expressions?
ARegex
BPattern
CMatcher
DString
✗ Incorrect
The Regex class in Kotlin is designed for regular expression operations.
What does Regex("abc").matches("abc") return?
Atrue
Bfalse
CThrows an error
Dnull
✗ Incorrect
The matches() function returns true if the entire string matches the pattern exactly.
Which function finds all matches of a pattern in a string?
AcontainsMatchIn()
Breplace()
Cmatches()
DfindAll()
✗ Incorrect
findAll() returns all occurrences of the pattern in the input string.
What does containsMatchIn() check?
AIf the pattern is empty
BIf the pattern appears anywhere in the string
CIf the string is empty
DIf the entire string matches the pattern
✗ Incorrect
containsMatchIn() returns true if the pattern is found anywhere inside the string.
How do you create a Regex to match any digit in Kotlin?
ARegex("[a-z]")
BRegex("d")
CRegex("\\d")
DRegex("\\w")
✗ Incorrect
The pattern \\d matches any digit character.
Explain how to use the Kotlin Regex class to check if a string contains a specific pattern.
Think about how to create a pattern and check if it appears anywhere in the text.
You got /3 concepts.
Describe the difference between the matches() and findAll() functions in the Regex class.
One checks the entire string, the other finds many matches inside.
You got /3 concepts.
Practice
(1/5)
1. What does the Regex.matches() function do in Kotlin?
easy
A. Finds the first occurrence of the pattern in the string
B. Splits the string by the regex pattern
C. Replaces all matches of the pattern with a new string
D. Checks if the entire input string matches the regex pattern
Solution
Step 1: Understand Regex.matches() purpose
The matches() function returns true only if the whole input string fits the pattern exactly.
Step 2: Differentiate from other Regex methods
Other methods like find() look for parts of the string, but matches() requires full match.
Final Answer:
Checks if the entire input string matches the regex pattern -> Option D
Quick Check:
matches() = full string match [OK]
Hint: matches() means full string must fit pattern exactly [OK]
Common Mistakes:
Thinking matches() finds partial matches
Confusing matches() with find()
Assuming matches() replaces text
2. Which of the following is the correct way to create a Regex object to match digits in Kotlin?
easy
A. val regex = Regex("\\d+")
B. val regex = Regex("\d+")
C. val regex = Regex("\d")
D. val regex = Regex("[d]+")
Solution
Step 1: Understand escaping in Kotlin strings
In Kotlin, backslash must be escaped, so to represent \d (digit), we write "\\d".
Step 2: Check the quantifier usage
"\\d+" means one or more digits, which is correct for matching digits.
Final Answer:
val regex = Regex("\\d+") -> Option A
Quick Check:
Double backslash needed for \d in Kotlin strings [OK]
Hint: Escape backslash twice in Kotlin regex strings [OK]
Common Mistakes:
Using single backslash without escaping
Confusing character class syntax
Missing quantifier '+' for multiple digits
3. What will be the output of this Kotlin code?
val regex = Regex("a.b")
val input = "acb aab aeb"
val matches = regex.findAll(input).map { it.value }.toList()
println(matches)
medium
A. ["acb", "aab", "aeb"]
B. ["acb", "aab"]
C. ["aab", "aeb"]
D. []
Solution
Step 1: Understand the regex pattern "a.b"
The dot '.' matches any single character, so "a.b" matches 'a' + any char + 'b'.
Step 2: Check input string matches
Input has "acb", "aab", and "aeb" all matching the pattern.
Final Answer:
["acb", "aab", "aeb"] -> Option A
Quick Check:
Dot matches any char between 'a' and 'b' [OK]
Hint: Dot '.' matches any single character in regex [OK]
Common Mistakes:
Thinking dot matches zero or multiple chars
Missing some matches in the list
Confusing findAll with matches
4. Identify the error in this Kotlin code snippet:
val regex = Regex("[a-z")
val result = regex.matches("abc")
medium
A. Missing escape for square brackets
B. Unclosed character class in regex pattern
C. matches() cannot be used with character classes
D. matches() requires a raw string literal
Solution
Step 1: Check regex pattern syntax
The pattern "[a-z" has an opening square bracket but no closing bracket, causing a syntax error.
Step 2: Understand consequences
Regex constructor will throw an exception due to invalid pattern.
Final Answer:
Unclosed character class in regex pattern -> Option B
Quick Check:
Character classes must be closed with ']' [OK]
Hint: Always close character classes with ']' in regex [OK]
Common Mistakes:
Forgetting to close square brackets
Thinking matches() needs raw strings
Confusing escape rules for brackets
5. You want to replace all sequences of one or more spaces in a string with a single dash ('-'). Which Kotlin Regex and method combination correctly does this?
hard
A. Regex("\\s+").replaceAll(input, "-")
B. Regex("\\s*").replace(input, "-")
C. Regex("\\s+").replace(input, "-")
D. Regex("[ ]+").replaceAll(input, "-")
Solution
Step 1: Choose correct regex pattern for spaces
"\\s+" matches one or more whitespace characters; correct for sequences of spaces.
Step 2: Use correct Kotlin Regex method
Kotlin's Regex class uses replace() to replace all matches; replaceAll() does not exist.
Final Answer:
Regex("\\s+").replace(input, "-") -> Option C
Quick Check:
Use replace() with "\\s+" to replace spaces [OK]
Hint: Use replace() with "\\s+" to replace space sequences [OK]
Common Mistakes:
Using replaceAll() which is not in Kotlin Regex
Using "\\s*" which matches zero spaces
Not escaping backslash properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.