Bird
Raised Fist0
Kotlinprogramming~10 mins

Regular expressions with Regex class in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Regex object that matches the word "cat".

Kotlin
val pattern = Regex([1])
println(pattern.matches("cat"))
Drag options to blanks, or click blank then click option'
A"cat"
B"dog"
C"bat"
D"rat"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the pattern inside quotes.
Using a wrong pattern string.
2fill in blank
medium

Complete the code to check if the string "hello123" contains digits using Regex.

Kotlin
val regex = Regex([1])
println(regex.containsMatchIn("hello123"))
Drag options to blanks, or click blank then click option'
A"[a-z]+"
B"\\d+"
C"[A-Z]+"
D"\\w+"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a pattern that matches only letters.
Not escaping the backslash properly.
3fill in blank
hard

Fix the error in the code to find all words starting with 'a' in the text.

Kotlin
val text = "apple banana apricot avocado"
val regex = Regex([1])
val matches = regex.findAll(text).map { it.value }.toList()
println(matches)
Drag options to blanks, or click blank then click option'
A"a+"
B"a*"
C"a\\w*"
D"a?"
Attempts:
3 left
💡 Hint
Common Mistakes
Using "a*" which matches zero or more 'a's anywhere.
Using "a+" which requires at least one 'a' but not the following letters.
4fill in blank
hard

Fill both blanks to create a Regex that matches email addresses and test it on a sample string.

Kotlin
val emailRegex = Regex([1])
val testEmail = "user@example.com"
println(emailRegex.[2](testEmail))
Drag options to blanks, or click blank then click option'
A"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"
Bmatches
CcontainsMatchIn
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using containsMatchIn() which checks partial matches.
Using an incomplete regex pattern.
5fill in blank
hard

Fill all three blanks to extract all numbers from a string and convert them to integers.

Kotlin
val input = "I have 2 apples and 10 bananas"
val numberRegex = Regex([1])
val numbers = numberRegex.findAll(input).map { it.[2].to[3]() }.toList()
println(numbers)
Drag options to blanks, or click blank then click option'
A"\\d+"
Bvalue
CInt
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' instead of 'value' to get the matched string.
Forgetting to convert the string to Int.

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

  1. Step 1: Understand Regex.matches() purpose

    The matches() function returns true only if the whole input string fits the pattern exactly.
  2. Step 2: Differentiate from other Regex methods

    Other methods like find() look for parts of the string, but matches() requires full match.
  3. Final Answer:

    Checks if the entire input string matches the regex pattern -> Option D
  4. 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

  1. Step 1: Understand escaping in Kotlin strings

    In Kotlin, backslash must be escaped, so to represent \d (digit), we write "\\d".
  2. Step 2: Check the quantifier usage

    "\\d+" means one or more digits, which is correct for matching digits.
  3. Final Answer:

    val regex = Regex("\\d+") -> Option A
  4. 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

  1. Step 1: Understand the regex pattern "a.b"

    The dot '.' matches any single character, so "a.b" matches 'a' + any char + 'b'.
  2. Step 2: Check input string matches

    Input has "acb", "aab", and "aeb" all matching the pattern.
  3. Final Answer:

    ["acb", "aab", "aeb"] -> Option A
  4. 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

  1. Step 1: Check regex pattern syntax

    The pattern "[a-z" has an opening square bracket but no closing bracket, causing a syntax error.
  2. Step 2: Understand consequences

    Regex constructor will throw an exception due to invalid pattern.
  3. Final Answer:

    Unclosed character class in regex pattern -> Option B
  4. 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

  1. Step 1: Choose correct regex pattern for spaces

    "\\s+" matches one or more whitespace characters; correct for sequences of spaces.
  2. Step 2: Use correct Kotlin Regex method

    Kotlin's Regex class uses replace() to replace all matches; replaceAll() does not exist.
  3. Final Answer:

    Regex("\\s+").replace(input, "-") -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard