Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

158 changes: 129 additions & 29 deletions module-1/Advanced-Regex/your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -27,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -36,10 +36,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"execution_count": 80,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['i', 'i', 'o', 'i', 'o', 'e', 'a', 'e', 'e', 'e', 'i', 'a', 'o', 'o', 'u', 'e', 'o', 'o', 'e', 'i', 'i']\n"
]
}
],
"source": [
"regex = re.findall('[aeiou]', text)\n",
"print(regex)"
]
},
{
"cell_type": "markdown",
Expand All @@ -50,19 +61,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"text = \"The puppy saw all the rest of the puppies playing and wanted to join them. I saw this and wanted a puppy of my own!\""
"text2 = \"The puppy saw all the rest of the puppies playing and wanted to join them. I saw this and wanted a puppy of my own!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"execution_count": 82,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n",
"<re.Match object; span=(4, 9), match='puppy'>\n",
"None\n",
"<re.Match object; span=(34, 41), match='puppies'>\n"
]
}
],
"source": [
"puppy_list = ['Puppy', 'puppy', 'Puppies', 'Puppies']\n",
"\n",
"a = re.search('Puppy',text2)\n",
"b = re.search('puppy',text2)\n",
"c = re.search('Puppies',text2)\n",
"d = re.search('puppies',text2)\n",
"\n",
"print(a)\n",
"print(b)\n",
"print(c)\n",
"print(d)\n",
"\n",
"#There are two occurences. 'puppy' and 'puppies'\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -73,19 +109,59 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"text = \"I ran the relay race the only way I knew how to run it.\""
"text3 = \"I ran the relay race the only way I knew how to run it.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<re.Match object; span=(48, 51), match='run'>\n",
"None\n",
"<re.Match object; span=(2, 5), match='ran'>\n"
]
}
],
"source": [
"\n",
"a = re.search('run',text3)\n",
"b = re.search('running',text3)\n",
"c = re.search('ran',text3)\n",
"\n",
"print(a)\n",
"print(b)\n",
"print(c)\n",
"\n",
"#there is an occurance of 'run' and 'ran'\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['I', 'ran', 'the', 'relay', 'race', 'the', 'only', 'way', 'I', 'knew', 'how', 'to', 'run', 'it.']\n"
]
}
],
"source": [
"text3_split = text3.split(' ')\n",
"print(text3_split)"
]
},
{
"cell_type": "markdown",
Expand All @@ -96,10 +172,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['r', 'r', 'r', 'r']"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#error\n",
"re.findall('r+', text3)\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -110,19 +200,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"text = \"Th!s !s a sentence w!th spec!al characters !n !t.\""
"text4 = \"Th!s !s a sentence w!th spec!al characters !n !t.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Th!s !s a sentence w!th spec!al characters !n !t.\n"
]
}
],
"source": [
"print(re.sub('i', '!', text4))\n"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -349,7 +449,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
"version": "3.8.3"
}
},
"nbformat": 4,
Expand Down
Loading