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 quotesyou 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.
- regexp =
/"([^"]+)",\s*"([^"]+)"/- regexp =
/"(.*)",\s*"(.*)"/- regexp =
/"(.*)", "(.*)"/- 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*"([^"]+)"/ "([^"]+)"= @ least 1 character not"surrounded",= comma\s*= number of spaces (including 0)"([^"]+)"= 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*"(.*)"/ "(.*)"= number (including 0) of almost any character surrounded".,= comma\s*= number of spaces (including 0)"(.*)"= 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:
"(.*)"= number (including 0) of character surrounded",= comma= single space"(.*)"= 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:
(.*)= number (including 0) of character,= comma\s*= number (including 0) of spaces(.*)= 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
Post a Comment