{"id":4350,"date":"2016-06-05T01:44:55","date_gmt":"2016-06-05T01:44:55","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4350"},"modified":"2016-06-05T01:44:55","modified_gmt":"2016-06-05T01:44:55","slug":"scala-regex-split-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-regex-split-example\/","title":{"rendered":"Scala regex split example"},"content":{"rendered":"<p><a href=\"https:\/\/www.garysieling.com\/scaladoc\/scala.util.matching.regex\/2016\/02\/15\/scala__util_matching_Regex.html\">Scala regular expressions<\/a> have a &#8220;split&#8221; function on them, which applies the regular expression to a string, breaking it up into an array of values from the original.<\/p>\n<p>For instance, in this example we&#8217;ll use commas as delimiters:<\/p>\n<pre lang=\"scala\">\n\",\".r.split(\"1,0,3\")\n<\/pre>\n<pre>\nres14: Array[String] = Array(1, 0, 3)\n<\/pre>\n<p>If you want to match repeated characters, as expected, you can use the &#8220;+&#8221; in the regular expression, and you&#8217;ll get the same result:<\/p>\n<pre lang=\"scala\">\n\",+\".r.split(\"1,0,,,,3\")\n<\/pre>\n<pre>\nres16: Array[String] = Array(1, 0, 3)\n<\/pre>\n<p>Note that you don&#8217;t want to use &#8220;*&#8221; on the string, as this matches 0 or more characters, so you&#8217;ll just get an array with every character in an entry:<\/p>\n<pre lang=\"scala\"> \n\",*\".r.split(\"1,0,,,,3\")\n<\/pre>\n<pre>\nres17: Array[String] = Array(1, \"\", 0, \"\", 3)\n<\/pre>\n<p>If you include a slash, you will need to escape it, like so:<\/p>\n<pre lang=\"scala\">\n\"\\\\w+\".r.split(\"1b0a3\")\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using &#8220;split&#8221; on scala regular expressions<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[460,480],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4350"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4350"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4350\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}