{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# RE module" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import re\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. To extract a pattern in a given string - Use `re.findall(pattern, text)`\n", "\n", "Returns a list containing all matches.\n", "\n", "In the example below we try to extract string of three consecutive numbers." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['619', '999', '871']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text = 'Jeswin 619 George 999 98Y Y27J0 JK871'\n", "re.findall(r'\\d{3}', text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now to get the first or last patterns use indexing." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'619'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.findall(r'\\d{3}', text)[0]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'871'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.findall(r'\\d{3}', text)[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. To substitute a pattern with a string of your choice - `re.sub(pattern, desired_string, text)`\n", "\n", "replaces the matches with the text of your choice." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "text = 'Jeswin 619 George 999 98Y Y27J0 JK871'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To replace __Y__ with string __B__." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jeswin 619 George 999 98B B27J0 JK871'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "re.sub('Y', \"B\", text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the given text, ensure that the alpha-numeric words doesnt have alphabets and resulting numeric word must have only numerics and must have length of 3 (You can use any numeric of your choice)." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "output = []\n", "for word in text.split():\n", " if re.findall(r'\\d', word):\n", " if re.findall(r'[A-Z]',word):\n", " word = re.sub(r'[A-Z]', '5', word)\n", " if len(word)==3:\n", " output.append(word)\n", " elif len(word)<3:\n", " while (3-len(word))<=0:\n", " word += str(np.random.randomint(9))\n", " output.append(word)\n", " elif len(word)>3:\n", " word = word[:3-len(word)]\n", " output.append(word)\n", " else:\n", " output.append(word)\n", " \n", " else:\n", " output.append(word)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Jeswin', '619', 'George', '999', '985', '527', '558']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Combining the list of words to a single sentence using [string join()](https://www.geeksforgeeks.org/python-string-join-method/)." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jeswin 619 George 999 985 527 558'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' '.join(output)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Jeswin 619 George 999 98Y Y27J0 JK871'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Reference\n", "\n", "![](images/1.PNG)\n", "\n", "![](images/2.PNG)\n", "\n", "![](images/3.PNG)" ] } ], "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.7" }, "vscode": { "interpreter": { "hash": "54db34dbb873d0124069a1b7e3692f2fcb3af91d00ed8e76b38ddecc02ef7a27" } } }, "nbformat": 4, "nbformat_minor": 2 }