{"id":4323,"date":"2016-06-08T02:39:59","date_gmt":"2016-06-08T02:39:59","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4323"},"modified":"2016-06-08T02:39:59","modified_gmt":"2016-06-08T02:39:59","slug":"scala-filter-regex","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-filter-regex\/","title":{"rendered":"Scala: Filter Strings and Lists with Regexes"},"content":{"rendered":"<p>Scala has a neat built-in function that turns a string into a regular expression (.r). If you use it on a regular expression with slashes in it, you&#8217;ll get errors like so:<\/p>\n<pre lang=\"scala\">\n\"\\w+\".r\n<console>:1: error: invalid escape character\n       \"\\w+\".r\n         ^\n<\/pre>\n<p>Thus for many regexes, it is preferable to use the multi-line string syntax, as this skips the escaping.<\/p>\n<pre lang=\"scala\">\nscala> \"\"\"\\w+\"\"\".r\nres43: scala.util.matching.Regex = \\w+\n<\/pre>\n<p>From this, we can do some neat things, like find the first word in a sentence:<\/p>\n<pre lang=\"scala\">\nx.findFirstMatchIn(\"abc def\")\nres44: Option[scala.util.matching.Regex.Match] = Some(abc)\n<\/pre>\n<p>We can also replace all matches in the string, so if we want to swap one word for another, we can:<\/p>\n<pre lang=\"scala\">\nx.replaceAllIn(\"abc def\", \"gary\")\nres46: String = gary gary\n<\/pre>\n<p>You can also apply this to every value in a list, if you want to filter to just items that match. For example, here we filter the list to just values with a single word:<\/p>\n<pre lang=\"scala\">\nList(\"multi word\", \"12345\", \"word\")\n  .filter(\n    \"^\\\\w+$\".r\n       .findFirstIn(_)\n       .isDefined)\n<\/pre>\n<pre>\nres2: List[String] = List(12345, word)\n<\/pre>\n<p>The underscore in allows use to simplify the code, since there is only one thing we&#8217;re testing on the value (this is equivalent to writing a lambda, that looks like x => &#8230;findFirstIn(x) ).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Examples of how to filter strings and lists in scala using 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":[480],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4323"}],"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=4323"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4323\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}