Skip to content

Instantly share code, notes, and snippets.

@slingam00
Last active October 22, 2021 16:55
Show Gist options
  • Save slingam00/11258d64663e4dcde237bd3fb8015694 to your computer and use it in GitHub Desktop.
Save slingam00/11258d64663e4dcde237bd3fb8015694 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Regular Expressions\n",
"\n",
"Regular Expression Sequence Cheat Sheet:\n",
"\n",
"https://cheatography.com/davechild/cheat-sheets/regular-expressions/\n",
"\n",
"Regular Expression Tester\n",
"\n",
"https://regex101.com"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import Statement"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import re # regular express"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## re.findall()\n",
"\n",
"Returns a list of occurrences of a pattern in a string"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['ab', 'ab', 'ab']\n",
"[]\n",
"['123']\n"
]
}
],
"source": [
"test = \"abcdabtesabt123\" # test string\n",
"\n",
"print(re.findall(\"ab\", test)) # matching characters 'ab'\n",
"\n",
"print(re.findall(\"x\", test)) # matching character 'x'\n",
"\n",
"print(re.findall(\"\\d+\", test)) # sequence of integers more than once"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## re.search()\n",
"\n",
"Returns the first matching object of a pattern in a string"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<re.Match object; span=(3, 4), match=' '>\n",
"<re.Match object; span=(9, 10), match='1'>\n"
]
}
],
"source": [
"test = \"The Rain 12345\" # test string\n",
"print(re.search(\"\\s\", test)) # matching first space character\n",
"print(re.search(\"\\d\", test)) # matching first integer character"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## re.split()\n",
"\n",
"Returns a list of the splitting of a string by a pattern"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['This', 'is', 'a', 'test']\n"
]
}
],
"source": [
"test = \"This is a test\" # test string\n",
"\n",
"print(re.split(\"\\s\", test)) # splitting a string by a space"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## re.sub()\n",
"\n",
"Returns back the string with the subbing of a pattern with another pattern"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123-456-789-abc-def-ghi\n",
"numbers numbers numbers abc def ghi\n"
]
}
],
"source": [
"test = \"123 456 789 abc def ghi\" # test string\n",
"\n",
"print(re.sub(\"\\s\", \"-\", test)) # subbing a space with a dash\n",
"\n",
"print(re.sub(\"\\d+\", \"numbers\", test)) # subbing a sequence of numbers with the string 'numbers'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Replacing values in the Iris Data Frame"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sepal_length</th>\n",
" <th>sepal_width</th>\n",
" <th>petal_length</th>\n",
" <th>petal_width</th>\n",
" <th>species</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5.1</td>\n",
" <td>3.5</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>s</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>4.9</td>\n",
" <td>3.0</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>s</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>4.7</td>\n",
" <td>3.2</td>\n",
" <td>1.3</td>\n",
" <td>0.2</td>\n",
" <td>s</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>4.6</td>\n",
" <td>3.1</td>\n",
" <td>1.5</td>\n",
" <td>0.2</td>\n",
" <td>s</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>5.0</td>\n",
" <td>3.6</td>\n",
" <td>1.4</td>\n",
" <td>0.2</td>\n",
" <td>s</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>6.7</td>\n",
" <td>3.0</td>\n",
" <td>5.2</td>\n",
" <td>2.3</td>\n",
" <td>virginica</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>6.3</td>\n",
" <td>2.5</td>\n",
" <td>5.0</td>\n",
" <td>1.9</td>\n",
" <td>virginica</td>\n",
" </tr>\n",
" <tr>\n",
" <th>147</th>\n",
" <td>6.5</td>\n",
" <td>3.0</td>\n",
" <td>5.2</td>\n",
" <td>2.0</td>\n",
" <td>virginica</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>6.2</td>\n",
" <td>3.4</td>\n",
" <td>5.4</td>\n",
" <td>2.3</td>\n",
" <td>virginica</td>\n",
" </tr>\n",
" <tr>\n",
" <th>149</th>\n",
" <td>5.9</td>\n",
" <td>3.0</td>\n",
" <td>5.1</td>\n",
" <td>1.8</td>\n",
" <td>virginica</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>150 rows × 5 columns</p>\n",
"</div>"
],
"text/plain": [
" sepal_length sepal_width petal_length petal_width species\n",
"0 5.1 3.5 1.4 0.2 s\n",
"1 4.9 3.0 1.4 0.2 s\n",
"2 4.7 3.2 1.3 0.2 s\n",
"3 4.6 3.1 1.5 0.2 s\n",
"4 5.0 3.6 1.4 0.2 s\n",
".. ... ... ... ... ...\n",
"145 6.7 3.0 5.2 2.3 virginica\n",
"146 6.3 2.5 5.0 1.9 virginica\n",
"147 6.5 3.0 5.2 2.0 virginica\n",
"148 6.2 3.4 5.4 2.3 virginica\n",
"149 5.9 3.0 5.1 1.8 virginica\n",
"\n",
"[150 rows x 5 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Using str.replace()\n",
"\n",
"data = pd.read_csv(\"iris.csv\")\n",
"\n",
"# replacing every 'setosa' species to 's'\n",
"data[\"species\"] = data[\"species\"].replace(\"setosa\", \"s\")\n",
"data"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment