Bird
0
0

What happens if a Ruby regex character class contains duplicate characters, like /[abcabc]/?

hard📝 Conceptual Q10 of 15
Ruby - Regular Expressions
What happens if a Ruby regex character class contains duplicate characters, like /[abcabc]/?
ADuplicates cause the class to match twice as often
BRegex raises a syntax error
CDuplicates are ignored; class matches a, b, or c
DOnly the first occurrence of each character is matched
Step-by-Step Solution
Solution:
  1. Step 1: Understand character class behavior with duplicates

    In Ruby regex, duplicate characters inside [] are ignored; the class matches any unique character listed.
  2. Step 2: Confirm no error or special behavior

    The regex /[abcabc]/ behaves the same as /[abc]/, matching a, b, or c without error or frequency change.
  3. Final Answer:

    Duplicates are ignored; class matches a, b, or c -> Option C
  4. Quick Check:

    Duplicate chars in [] are ignored [OK]
Quick Trick: Duplicate chars in [] don't affect matching [OK]
Common Mistakes:
  • Expecting syntax error from duplicates
  • Thinking duplicates increase match frequency
  • Assuming only first occurrence is matched

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes