{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Previous

\n", "

Next

\n", "

Tour of Scala

\n", "
\n", "\n", "# Regular Expression Patterns\n", "\n", "Regular expressions are strings which can be used to find patterns (or lack thereof) in data. Any string can be converted to a regular expression using the `.r` method." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Password must contain a number\n" ] }, { "data": { "text/plain": [ "\u001b[32mimport \u001b[39m\u001b[36mscala.util.matching.Regex\n", "\n", "\u001b[39m\n", "\u001b[36mnumberPattern\u001b[39m: \u001b[32mRegex\u001b[39m = [0-9]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import scala.util.matching.Regex\n", "\n", "val numberPattern: Regex = \"[0-9]\".r\n", "\n", "numberPattern.findFirstMatchIn(\"awesomepassword\") match {\n", " case Some(_) => println(\"Password OK\")\n", " case None => println(\"Password must contain a number\")\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above example, the `numberPattern` is a `Regex`\n", "(regular expression) which we use to make sure a password contains a number.\n", "\n", "You can also search for groups of regular expressions using parentheses." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "key: background-color value: #A03300\n", "key: background-image value: url(img\n", "key: background-position value: top center\n", "key: background-repeat value: repeat-x\n", "key: background-size value: 2160px 108px\n", "key: margin value: 0\n", "key: height value: 108px\n", "key: width value: 100\n" ] }, { "data": { "text/plain": [ "\u001b[32mimport \u001b[39m\u001b[36mscala.util.matching.Regex\n", "\n", "\u001b[39m\n", "\u001b[36mkeyValPattern\u001b[39m: \u001b[32mRegex\u001b[39m = ([0-9a-zA-Z-#() ]+): ([0-9a-zA-Z-#() ]+)\n", "\u001b[36minput\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"\"\"background-color: #A03300;\n", "background-image: url(img/header100.png);\n", "background-position: top center;\n", "background-repeat: repeat-x;\n", "background-size: 2160px 108px;\n", "margin: 0;\n", "height: 108px;\n", "width: 100%;\"\"\"\u001b[39m" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import scala.util.matching.Regex\n", "\n", "val keyValPattern: Regex = \"([0-9a-zA-Z-#() ]+): ([0-9a-zA-Z-#() ]+)\".r\n", "\n", "val input: String =\n", " \"\"\"background-color: #A03300;\n", " |background-image: url(img/header100.png);\n", " |background-position: top center;\n", " |background-repeat: repeat-x;\n", " |background-size: 2160px 108px;\n", " |margin: 0;\n", " |height: 108px;\n", " |width: 100%;\"\"\".stripMargin\n", "\n", "for (patternMatch <- keyValPattern.findAllMatchIn(input))\n", " println(s\"key: ${patternMatch.group(1)} value: ${patternMatch.group(2)}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we parse out the keys and values of a String. Each match has a group of sub-matches. Here is the output:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```\n", "key: background-color value: #A03300\n", "key: background-image value: url(img\n", "key: background-position value: top center\n", "key: background-repeat value: repeat-x\n", "key: background-size value: 2160px 108px\n", "key: margin value: 0\n", "key: height value: 108px\n", "key: width value: 100\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Previous

\n", "

Next

\n", "

Tour of Scala

\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Scala (2.13)", "language": "scala", "name": "scala213" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala", "nbconvert_exporter": "script", "version": "2.13.1" } }, "nbformat": 4, "nbformat_minor": 4 }