Ruby - Regular ExpressionsHow 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/Check Answer
Step-by-Step SolutionSolution:Step 1: Understand character class for letters and digits\w matches letters, digits, and underscore. Using [\w]+ matches one or more such characters.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.Final Answer:/\A[\w]+\z/ matches strings with only letters and digits, at least one char -> Option DQuick 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 stringNot anchoring regex, allowing partial matchesUsing \d+ which matches digits only
Master "Regular Expressions" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Advanced Metaprogramming - Module_eval for dynamic behavior - Quiz 5medium Concurrent Programming - Ractor for true parallelism - Quiz 3easy Functional Patterns in Ruby - Why functional patterns complement OOP - Quiz 9hard Functional Patterns in Ruby - Proc composition - Quiz 15hard Gems and Bundler - Why gem management matters - Quiz 13medium Metaprogramming Fundamentals - Open struct for dynamic objects - Quiz 15hard Regular Expressions - Match operator (=~) - Quiz 1easy Ruby Ecosystem and Best Practices - Ruby version management (rbenv, rvm) - Quiz 4medium Testing with RSpec and Minitest - RSpec describe and it blocks - Quiz 5medium Testing with RSpec and Minitest - Minitest basics (assert style) - Quiz 11easy