Regular Expressions Flashcards
What are the main uses of regular expressions in Ruby?
Two common use cases for regular expressions include
1) validation
2) parsing.
What are the shorthand syntax for the following character ranges?
\w
\d
\s
\w is equivalent to [0-9a-zA-Z_]
\d is the same as [0-9]
\s matches white space (tabs, regular space, newline)
What are the shorthand syntax for the following negative character ranges?
\W
\D
\S
\W anything that’s not in [0-9a-zA-Z_]
\D anything that’s not a number
\S anything that’s not a space
The operator which is same as “match” method?
=~ operator
Which of the following is true?
defining a regular expression:
Regexp.new( ‘string pattern’ [, options ] )
/string pattern/
%r{string pattern}
All are true
What is the dedicated class for Regular Expression in ruby?
Regexp
Explain the functions of the following modifiers?
+
*
{3,5}
+ 1 or more(at least one of anything (except a newline))
* 0 or more (any character (except a newline)
{3,5} between 3 and 5
What is the use of the method “named_capture” in Regexp?
It will return a hash representing information about named captures of rxp.
A key of the hash is a name of the named captures. A value of the hash is an array which is list of indexes of corresponding named captures.
/(?.)(?.)/.named_captures #=> {"foo"=>[1], "bar"=>[2]}
Which method will return a list of names of captures as an array of strings.?
name method.
(?.)(?.)(?.)/.names #=> ["foo", "bar", "baz"]
What is the use of “source” method?
It will return the orginal string from the expression
/ab+c/ix.source #=> “ab+c”