Regular Expressions In Ruby

Name: _____________________

Date: _____________________

Instructions: Answer all questions. Write your answers clearly in the space provided.

Question 1:

What does the =~ operator do in Ruby when used with regular expressions?

A. Evaluates the regular expression
B. Compares two regular expressions
C. Matches a string against a regular expression
D. Assigns a regular expression to a string
Answer: _________
Question 2:

Which method is used to find the first occurrence of a pattern in a string in Ruby?

A. String#search
B. String#find
C. String#find_pattern
D. String#match
Answer: _________
Question 3:

What does the /A anchor represent in a regular expression in Ruby?

A. Matches one or more occurrences of the previous character
B. Matches any character except a newline
C. Matches the start of a string
D. Matches the end of a string
Answer: _________
Question 4:

What is the purpose of the + quantifier in a regular expression in Ruby?

A. Matches zero or one occurrence of the preceding character
B. Matches zero or more occurrences of the preceding character
C. Matches exactly n occurrences of the preceding character
D. Matches one or more occurrences of the preceding character
Answer: _________
Question 5:

Which modifier is used to perform case-insensitive matching in Ruby regular expressions?

A. i
B. m
C. x
D. o
Answer: _________
Question 6:

What does the d character class represent in a regular expression in Ruby?

A. Matches any word character
B. Matches any whitespace character
C. Matches any non-digit character
D. Matches any digit character
Answer: _________
Question 7:

Which metacharacter is used to specify optional characters in a regular expression in Ruby?

A. *
B. +
C. ?
D. .
Answer: _________
Question 8:

What does the ^ anchor represent in a regular expression in Ruby?

A. Matches the start of a string
B. Matches the start of a line
C. Matches the end of a string
D. None of the above
Answer: _________
Question 9:

Which method is used to replace occurrences of a pattern in a string with a specified string in Ruby?

A. String#modify
B. String#sub
C. String#gsub
D. String#replace
Answer: _________
Question 10:

What does the s character class represent in a regular expression in Ruby?

A. Matches any non-whitespace character
B. Matches any digit character
C. Matches any word character
D. Matches any whitespace character
Answer: _________
Question 11:

Which modifier is used to perform multiline matching in Ruby regular expressions?

A. m
B. i
C. x
D. o
Answer: _________
Question 12:

What does the x08 anchor represent in a regular expression in Ruby?

A. Matches a non-word character
B. Matches a non-word boundary
C. Matches a word character
D. Matches a word boundary
Answer: _________
Question 13:

What is the purpose of the * quantifier in a regular expression in Ruby?

A. Matches one or more occurrences of the preceding character
B. Matches one or zero occurrences of the preceding character
C. Matches zero or more occurrences of the preceding character
D. Matches exactly n occurrences of the preceding character
Answer: _________
Question 14:

What does the w character class represent in a regular expression in Ruby?

A. Matches any whitespace character
B. Matches any word character
C. Matches any digit character
D. None of the above
Answer: _________
Question 15:

Which method is used to extract all occurrences of a pattern in a string in Ruby?

A. String#scan
B. String#extract
C. String#find_all
D. String#match_all
Answer: _________
Question 16:

What does the $ anchor represent in a regular expression in Ruby?

A. Matches the start of a string
B. Matches the end of a string
C. Matches the start of a line
D. Matches the end of a line
Answer: _________
Question 17:

Which metacharacter is used to specify character ranges in a regular expression in Ruby?

A. ()
B. {}
C. []
D. <>
Answer: _________
Question 18:

Which modifier is used to perform global matching in Ruby regular expressions?

A. x
B. g
C. o
D. None of the above
Answer: _________
Question 19:

What does the A anchor represent in a regular expression in Ruby?

A. Matches one or more occurrences of the previous character
B. Matches any character except a newline
C. Matches the start of a string
D. Matches the end of a string
Answer: _________
Question 20:

What is the purpose of the G anchor in a regular expression in Ruby?

A. Matches the start of a string
B. Matches the end of a string
C. Matches any character except a newline
D. Matches the point where the last match ended
Answer: _________
Question 21:

Which metacharacter is used to specify grouping in a regular expression in Ruby?

A. ()
B. []
C. {}
D. <>
Answer: _________
Question 22:

Which modifier is used to perform single-line matching in Ruby regular expressions?

A. o
B. x
C. m
D. s
Answer: _________
Question 23:

What does the (?<=...) construct represent in a regular expression in Ruby?

A. Negative lookahead assertion
B. Negative lookbehind assertion
C. Positive lookbehind assertion
D. Positive lookahead assertion
Answer: _________
Question 24:

Which method is used to split a string into substrings based on a regular expression in Ruby?

A. String#break
B. String#segment
C. String#split
D. String#divide
Answer: _________
Question 25:

What does the (?<!...) construct represent in a regular expression in Ruby?

A. Positive lookbehind assertion
B. Negative lookahead assertion
C. Positive lookahead assertion
D. Negative lookbehind assertion
Answer: _________
Question 26:

Which metacharacter is used to specify the beginning of a line in a regular expression in Ruby?

A. ^
B. $
C. A
D. x1a
Answer: _________

Answer Key

1: C
2: D
3: C
4: D
5: A
6: D
7: C
8: B
9: C
10: D
11: A
12: D
13: C
14: B
15: A
16: D
17: C
18: B
19: C
20: D
21: A
22: D
23: C
24: C
25: D
26: A