variants for regex in ruby
I found all the regex syntaxes below can work in ruby.
irb(main):011:0> fruits = ["black berry 12","cherry 9","kiwi peach 15"]
=> ["black berry 12", "cherry 9", "kiwi peach 15"]
irb(main):013:0> fruits.map {|s| s.match(/(.+?)\s+(\d+)/); [$~[1],$~[2]]}
=> [["black berry", "12"], ["cherry", "9"], ["kiwi peach", "15"]]
irb(main):015:0> fruits.map {|s| s =~ /(.+?)\s+(\d+)/; [$1,$2]}
=> [["black berry", "12"], ["cherry", "9"], ["kiwi peach", "15"]]
irb(main):017:0> fruits.map {|s| /(.+?)\s+(\d+)/ =~ s; [$1,$2]}
=> [["black berry", "12"], ["cherry", "9"], ["kiwi peach", "15"]]
It's too flexible though.
In scala just one syntax in normal.
scala> val fruits = List("black berry 12","cherry 9","kiwi peach 15")
val fruits: List[String] = List(black berry 12, cherry 9, kiwi peach 15)
scala> val regex= """(.+?)\s+(\d+)""".r
val regex: scala.util.matching.Regex = (.+?)\s+(\d+)
scala> fruits.map{ case regex(x,y) => (x,y) }
val res0: List[(String, String)] = List((black berry,12), (cherry,9), (kiwi peach,15))