{"id":4335,"date":"2016-06-04T01:41:52","date_gmt":"2016-06-04T01:41:52","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4335"},"modified":"2016-06-04T01:41:52","modified_gmt":"2016-06-04T01:41:52","slug":"scala-regex-examples","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/scala-regex-examples\/","title":{"rendered":"Scala regex examples"},"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","protected":false},"excerpt":{"rendered":"<p>Examples of using regular expressions in Scala<\/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\/4335"}],"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=4335"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4335\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}