Pacific-Design.com

    
Home Index

1. Scala

2. 3b Regex

Scala / 3b Regex /

Scala - Regex Parse Split Values

import scala.util.matching.Regex

object App {

  def main(args: Array[String]) {

    findPeriod
  }

  def findPeriod: Unit = {
    val row =
      """platform_content_id=zvUgTzKyPr4
        |date=2016-05-04
        |platform_name=YT
        |video_title=2011 Toyota Camry LE 6-Spd AT - for sale in Hollywood, FL 33
        |description=null
        |category_id=2
        |channel_id=UC2NNrtFvMlGPH225rp_6KbQ
        |channel_title=null
        |published_at=2013-09-14
        |video_definition=sd
        |dimension=2d
        |duration=137
        |licensed_content=f
        |is_claimed=null
        |has_snippet=t
        |has_statistics=t
        |has_content_details=t
        |last_update_time=2015-02-13 02:03:51.795494
        |is_private=null
        |is_taken_down=null
        |record_creation_time=2015-02-12 20:53:45.452213
        |comments=null
        |dislikes=null
        |likes=null
        |views=null
        |period=over3years days21 rank50"""

    val pattern = new Regex("period=\\w+ \\w+ \\w+")
    val date: Option[String] = pattern.findFirstIn(row) //.toString //.split("rank")
    val res1 = date.map(_.split("rank").toList)
    println(res1.get(1))

  }



  def findDate: Unit = {
    val row =
      """date=2016-05-04
        |platform_name=YT
        |platform_content_id=j4lhO3wV28U
        |video_title=Rossini   La Cenerentola   Atto Primo   Nel volto estatico
        |description=null
        |category_id=24
        |channel_id=UCzIpmE9bTQVH8rLPMw3e8dg
        |channel_title=null
        |published_at=2014-03-20
        |video_definition=sd
        |dimension=2d
        |duration=257
        |licensed_content=f
        |is_claimed=t
        |has_snippet=t
        |has_statistics=t
        |has_content_details=t
        |last_update_time=2015-04-07
        |is_private=null
        |is_taken_down=null
        |record_creation_time=2015-04-07
        |relevancy_score=null
        | """

    val pattern = new Regex("(date=)(\\w+-\\w+-\\w+)")
    val date = pattern.findFirstIn(row) //.toString //.split("=")
    println(date)

  }

}