str = "65101km,Sedan,Manual,18131A,FWD,Used,5.5L/100km,Toyota,camry,SE,{AC,Heated Seats, Heated Mirrors, Keyless Entry},2010"
r = /
(?<=\A|,) # match the beginning of the string or a comma in a positive lookbehind
(?: # begin a non-capture group
{.*?} # match an open brace followed by any number of characters,
# lazily, followed by a closed brace
| # or
.*? # match any number of characters, lazily
) # close non-capture group
(?=,|\z) # match a comma or the end of the string in a positive lookahead
/x # free-spacing regex definition mode
str.scan r
#=> ["65101km", "Sedan", "Manual", "18131A", "FWD", "Used", "5.5L/100km", "Toyota",
# "camry", "SE", "{AC,Heated Seats, Heated Mirrors, Keyless Entry}", "2010"]
Далее следуют два примечания.Я проиллюстрирую это более простой строкой.
str = "65101km,Sedan,{AC,Heated Seats},2010"
{.*?}
должно предшествовать .*?
в (?:{.*?}|.*?)
Если
r = /(?<=\A|,)(?:.*?|{.*?})(?=,|\z)/
затем
str.scan r
#=> ["65101km", "Sedan", "{AC", "Heated Seats}", "2010"]
Совпадения .*
должны быть ленивыми (иначе не жадными )
Если
r = /(?<=\A|,)(?:{.*?}|.*)(?=,|\z)/
, то
str.scan r
#=> ["65101km,Sedan,{AC,Heated Seats},2010"]
Если
r = /(?<=\A|,)(?:{.*}|.*?)(?=,|\z)/
, то
"65101km,Sedan,{AC,Heated Seats},2010,{starter motor, pneumatic tires}".scan r
#=> ["65101km", "Sedan", "{AC,Heated Seats},2010,{starter motor, pneumatic tires}"]