{ "cells": [ { "cell_type": "markdown", "id": "cb10925c-cccb-420e-be3b-b5ca49ad5cf5", "metadata": { "tags": [] }, "source": [ "# Splitting 'ref' into book, chapter, verse and word \n", "\n", "The XML source data contains a tag called 'ref' which contains information related to book, chapter, verse and word. See following snippet of XML:\n", "\n", "```\n", " ἐκλείσθη\n", "```\n", "\n", "This small Jupyter Notebook explains how the compound variable 'ref' can be split into its four constituent components:" ] }, { "cell_type": "code", "execution_count": 1, "id": "15d731b8-ac0e-4427-a998-29a2997d72b6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['MAT', '25', '10', '18']\n" ] } ], "source": [ "import re\n", "input=\"MAT 25:10!18\"\n", "x= re.sub(r'[!: ]',\" \", input).split()\n", "print (x)" ] }, { "cell_type": "markdown", "id": "d1a89bf7-1aa6-4762-99ab-cf2e677946ec", "metadata": {}, "source": [ "## Explanation of the code:\n", "\n", "The code begins by importing the regular expression module re to work with regular expressions.\n", "\n", "* The variable input is initialized with the string \"MAT 1:1!1\". This string contains various punctuation marks and spaces.\n", "\n", "* The re.sub() function is used to substitute certain characters in the input string with a space character (\" \"). The regular expression pattern [!: ] matches any occurrence of either a colon (:), an exclamation mark (!), or a space character. The matched characters are replaced with a space.\n", "\n", "* The result of the substitution operation is assigned to the variable x. This variable now holds the modified string where the matched characters have been replaced with spaces.\n", "\n", "* The split() method is then called on the modified string x. This method splits the string into a list of substrings based on whitespace. Since no specific delimiter is provided to the split() method, it uses whitespace (spaces) as the default delimiter.\n", "\n", "* The resulting list, containing the substrings after the split operation, is printed using the print() function." ] }, { "cell_type": "markdown", "id": "a5e4bdf3-b108-4c6a-b99b-a4bf4723afee", "metadata": {}, "source": [ "The following site can be used to build and verify a regular expression: [regex101.com](https://regex101.com/) (choose the 'Pyton flavor') " ] }, { "cell_type": "code", "execution_count": null, "id": "c2c4075b-6327-4ba2-ac8d-80b629b5620b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }