0
0
Kotlinprogramming~5 mins

Regular expressions with Regex class in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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
What does Regex("abc").matches("abc") return?
Atrue
Bfalse
CThrows an error
Dnull
Which function finds all matches of a pattern in a string?
AcontainsMatchIn()
Breplace()
Cmatches()
DfindAll()
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
How do you create a Regex to match any digit in Kotlin?
ARegex("[a-z]")
BRegex("d")
CRegex("\\d")
DRegex("\\w")
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.