Bird
0
0

How to write a Ruby regex that matches a string containing only letters and digits, at least one character long?

hard📝 Application Q9 of 15
Ruby - Regular Expressions
How to write a Ruby regex that matches a string containing only letters and digits, at least one character long?
A/\A\d+\z/
B/[a-zA-Z0-9]{0,}/
C/\w*/
D/\A[\w]+\z/
Step-by-Step Solution
Solution:
  1. Step 1: Understand character class for letters and digits

    \w matches letters, digits, and underscore. Using [\w]+ matches one or more such characters.
  2. Step 2: Use anchors to match entire string

    \A and \z anchor start and end of string, ensuring the whole string matches only these characters.
  3. Final Answer:

    /\A[\w]+\z/ matches strings with only letters and digits, at least one char -> Option D
  4. Quick Check:

    Anchored \w+ matches letters/digits only [OK]
Quick Trick: Use \A and \z to anchor full string match [OK]
Common Mistakes:
  • Using * quantifier allowing empty string
  • Not anchoring regex, allowing partial matches
  • Using \d+ which matches digits only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes