regex - Ruby regular expressions for movie titles and ratings -


the quiz problem:

you given following short list of movies exported excel comma-separated values (csv) file. each entry single string contains movie name in double quotes, 0 or more spaces, , movie rating in double quotes. example, here list 3 entries:

movies = [   %q{"aladdin",  "g"},   %q{"i, robot", "pg-13"},   %q{"star wars","pg"} ] 

your job create regular expression parse list:

movies.each |movie|   movie.match(regexp)   title,rating = $1,$2 end # => first entry, title should aladdin, rating should g, # => without double quotes 

you may assume movie titles , ratings never contain double-quote marks. within single entry, variable number of spaces (including 0) may appear between comma after title , opening quote of rating.

which of following regular expressions accomplish this? check apply.

  1. regexp = /"([^"]+)",\s*"([^"]+)"/
  2. regexp = /"(.*)",\s*"(.*)"/
  3. regexp = /"(.*)", "(.*)"/
  4. regexp = /(.*),\s*(.*)/

would explain why answer (1) , (2)?

would explain why answer (1) , (2)?

the resulting strings similar "aladdin", "g" let's take @ correct answer #1:

/"([^"]+)",\s*"([^"]+)"/ 
  1. "([^"]+)" = @ least 1 character not " surrounded "
  2. , = comma
  3. \s* = number of spaces (including 0)
  4. "([^"]+)" = first

which type of strings get. let's take @ above string:

 "aladdin",   "g" #^1       ^2^3^4 

now let's take @ second correct answer:

/"(.*)",\s*"(.*)"/ 
  1. "(.*)" = number (including 0) of almost any character surrounded ".
  2. , = comma
  3. \s* = number of spaces (including 0)
  4. "(.*)" = see first point

which correct following irb session (using ruby 1.9.3) shows:

'"aladdin",   "g"'.match(/"([^"]+)",\s*"([^"]+)"/) # number 1 # => #<matchdata "\"aladdin\",   \"g\"" 1:"aladdin" 2:"g"> '"aladdin",   "g"'.match(/"(.*)",\s*"(.*)"/) # number 2 # => #<matchdata "\"aladdin\",   \"g\"" 1:"aladdin" 2:"g">   

just completeness i'll tell why third , fourth wrong well:

/"(.*)", "(.*)"/ 

the above regex is:

  1. "(.*)" = number (including 0) of character surrounded "
  2. , = comma
  3. = single space
  4. "(.*)" = see first point

which wrong because, example, aladdin takes more 1 character (the first point) following irb session shows:

'"aladdin",   "g"'.match(/"(.*)", "(.*)"/) # number 3 # => nil  

the fourth regex is:

/(.*),\s*(.*)/ 

which is:

  1. (.*) = number (including 0) of character
  2. , = comma
  3. \s* = number (including 0) of spaces
  4. (.*) = see first point

which wrong because text explicitly says movie titles not contain number of " character , surrounded double quotes. above regex not checks presence of " in movie titles needed surrounding double quotes, accepting strings "," (which not valid) following irb session shows:

'","'.match(/(.*),\s*(.*)/) # number 4 # => #<matchdata "\",\"" 1:"\"" 2:"\"">  

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -