Here are some notes on regular expressions that frequently appear in the Ruby Silver exam.
The reference used is this book. The following examples focus on regular expressions that appeared in the practice problems and mock exams of this study guide.
Revised 2nd Edition: Ruby Certification Exam Study Guide (Silver/Gold)
Surprisingly, there are only four questions on regular expressions. However, since they are guaranteed to appear, it’s important to understand them.
Below are actual questions from the Revised 2nd Edition: Ruby Certification Exam Study Guide (Silver/Gold).
Metacharacter | Meaning |
---|---|
[] | Creates a character class to match any one character |
[^] | Creates a character class to match any one character except those specified |
- | Represents a range of characters when used inside [] |
. | Matches any single character |
() | Captures or groups matched strings |
? | Matches the preceding character or pattern 0 or 1 time |
* | Matches the preceding character or pattern 0 or more times |
+ | Matches the preceding character or pattern 1 or more times |
{n,m} | Matches the preceding character or pattern between n and m times |
| | Creates an OR condition |
^ | Matches the start of a line |
$ | Matches the end of a line |
\ | Escapes metacharacters or forms part of other metacharacters like \n or \w |
Choose the regular expressions that match lines consisting only of “New” or “new” (select two).
Options:
Answer:
Explanation:
Choose the regular expression that matches lines consisting only of one or more digits.
Options:
Answer:
Explanation:
What is the output of the following code?
p "abc def 123 ghi 456".scan(/\d+/).length
Answer:
Explanation:
What is the output of the following code?
p "HogeHOGEhoge"[/[A-Z][^A-Z]+/]
Answer:
Explanation: