diff --git a/info4/kapitel-2/RegulaereAusdruecke.ipynb b/info4/kapitel-2/RegulaereAusdruecke.ipynb index b59b20a17569fce799d0c883ce896618e1d1ed3e..c319f56740204365459e5f7112bae1f933499d0d 100644 --- a/info4/kapitel-2/RegulaereAusdruecke.ipynb +++ b/info4/kapitel-2/RegulaereAusdruecke.ipynb @@ -7,20 +7,32 @@ "# Reguläre Ausdrücke\n", "\n", "Sei $\\Sigma$ ein Alphabet. \n", - "Die __Menge der regulären Ausdrücke__ (\"uber\n", + "Die __Menge der regulären Ausdrücke__ (über\n", " $\\Sigma$) ist definiert durch:\n", - "* $\\emptyset$ und $\\lambda$ sind reguläre Ausdr\"ucke.\n", + "* $\\emptyset$ und $\\lambda$ sind reguläre Ausdrücke.\n", "* Jedes $a \\in \\Sigma$ ist ein regulärer Ausdruck.\n", - "* Sind $\\alpha$ und $\\beta$ reguläre Ausdr\"ucke, so sind auch \n", + "* Sind $\\alpha$ und $\\beta$ reguläre Ausdrücke, so sind auch \n", " * $\\alpha \\beta$, \n", " * $(\\alpha + \\beta)$ und \n", " * $(\\alpha)^{\\ast}$ \n", - "* regul\"are Ausdrücke.\n", + "* reguläre Ausdrücke.\n", "* Nichts sonst ist ein regulärer Ausdruck. \n", "\n", "\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Um mit den praktischen Aspekten der regulären Ausdrücke zu experimentieren laden wir eine Bibliothek.\n", + "In dieser Bibliothek wird der Plus Operator anders geschrieben: ```a|b```.\n", + "Es gibt nicht den regulären Ausdruck $\\emptyset$, aber dafür viele andere (abgeleitete) Operatoren (syntaktische Zucker), zum Beispiel:\n", + "* $\\alpha^+$ als Kürzel für $\\alpha(\\alpha)^*$\n", + "* [a-z] als Kürzel für $(a+b+c+d+...+z)$\n", + "\n" + ] + }, { "cell_type": "code", "execution_count": 19, @@ -64,9 +76,102 @@ ":init" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Mit ```REGEX_MATCH(w,r)``` können wir prüfen ob ein Wort ```w``` von einem regulären Ausdruck ```r``` generiert wird:" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/markdown": [ + "$\\mathit{TRUE}$" + ], + "text/plain": [ + "TRUE" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "REGEX_MATCH(\"a\",\"a\")" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/markdown": [ + "$\\mathit{TRUE}$" + ], + "text/plain": [ + "TRUE" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "REGEX_MATCH(\"b\",\"a|b\")" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/markdown": [ + "$\\mathit{TRUE}$" + ], + "text/plain": [ + "TRUE" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "REGEX_MATCH(\"aa\",\"a*\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "REGEX_MATCH(\"\",\"a*\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Mit ```REGEX_SEARCH(w,i,r)``` können wir prüfen ob in einem Wort ```w``` ab der Position ```i``` ein Teilwort von ```r``` generiert wird.\n", + "Wenn ja werden die erste Position und das längste Teilwort das von dort aus akzeptiert wird ausgegeben." + ] + }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 48, "metadata": {}, "outputs": [ { @@ -78,7 +183,7 @@ "rec(length∈3,position∈1,string∈\"aab\",submatches∈∅)" ] }, - "execution_count": 21, + "execution_count": 48, "metadata": {}, "output_type": "execute_result" } @@ -87,6 +192,36 @@ "REGEX_SEARCH(\"aabc\",1,\"a*b\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Mit Mit ```REGEX_SEARCH_ALL(w,r)``` können wir in einem Wort ```w``` nach allen längsten Teilworten suchen die von ```r``` generiert werden." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/markdown": [ + "$\\{(1\\mapsto\\text{\"aab\"}),(2\\mapsto\\text{\"bb\"}),(3\\mapsto\\text{\"a\"})\\}$" + ], + "text/plain": [ + "{(1↦\"aab\"),(2↦\"bb\"),(3↦\"a\")}" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "REGEX_SEARCH_ALL(\"aabcbbca\",\"(a|b)+\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -221,6 +356,15 @@ "REGEX_SEARCH_ALL(txt,\"G[a-z]*\")" ] }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + ":unlet txt" + ] + }, { "cell_type": "code", "execution_count": null,