Regular Expressions Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What are the main uses of regular expressions in Ruby?

A

Two common use cases for regular expressions include

1) validation
2) parsing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the shorthand syntax for the following character ranges?
\w
\d
\s

A

\w is equivalent to [0-9a-zA-Z_]

\d is the same as [0-9]

\s matches white space (tabs, regular space, newline)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the shorthand syntax for the following negative character ranges?
\W
\D
\S

A

\W anything that’s not in [0-9a-zA-Z_]

\D anything that’s not a number

\S anything that’s not a space

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The operator which is same as “match” method?

A

=~ operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which of the following is true?
defining a regular expression:

Regexp.new( ‘string pattern’ [, options ] )
/string pattern/
%r{string pattern}

A

All are true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the dedicated class for Regular Expression in ruby?

A

Regexp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the functions of the following modifiers?

+
*
{3,5}

A

+ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the use of the method “named_capture” in Regexp?

A

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]}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which method will return a list of names of captures as an array of strings.?

A

name method.

(?.)(?.)(?.)/.names
#=> ["foo", "bar", "baz"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the use of “source” method?

A

It will return the orginal string from the expression

/ab+c/ix.source #=> “ab+c”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly