{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e1db59fe",
   "metadata": {},
   "source": [
    "# In-Class Exercises, Session 4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55f57173",
   "metadata": {},
   "source": [
    "MATCHING"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd116d9d",
   "metadata": {},
   "source": [
    "<B>4.1:  <U>Match a simple character pattern</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6d752ea",
   "metadata": {},
   "source": [
    "Search for 'Velas', then try 'Benter' and 'Acme'.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aed60852",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "lines = [\n",
    "  'Acme Corporation is heded by CEO Joseph Benter, and ',\n",
    "  'President Maria Velas.  Mr. Benter focuses on R&D ',\n",
    "  'while Ms. Velas provides vision and major deals for ',\n",
    "  'Acme.  ']\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'', line):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e01fef48",
   "metadata": {},
   "source": [
    "<U>Expected Output</U> (for <B>Velas</B>):"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62245b3b",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">President Maria Velas.  Mr. Benter focuses on R&D\n",
    "while Ms. Velas provides vision and major deals for</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "743a36fc",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b80aafa",
   "metadata": {},
   "source": [
    "<B>4.2:  <U>'not' to negate a search</U>.  Execute previous pattern with 'not' in front of <B>re.search()</B></B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4743fc34",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "lines = [\n",
    "  'Acme Corporation is heded by CEO Joseph Benter, and ',\n",
    "  'President Maria Velas.  Mr. Benter focuses on R&D ',\n",
    "  'while Ms. Velas provides vision and major deals for ',\n",
    "  'Acme.  ']\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'Benter', line):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cc119d9",
   "metadata": {},
   "source": [
    "<U>Expected Output</U> (for <B>Benter</B>):"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d208f1ee",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">while Ms. Velas provides vision and major deals for\n",
    "Acme.</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02158f51",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "393e6d76",
   "metadata": {},
   "source": [
    "ANCHORS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1ffc43b",
   "metadata": {},
   "source": [
    "<B>4.3:  <U>Anchors - start of string</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87f02d4f",
   "metadata": {},
   "source": [
    "Print only those lines that have 'TEL' at the start:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f15de6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "for text_line in ['AURORA HOTEL',\n",
    "                  'OPEN12:00 AM - 11:59 PM',\n",
    "                  '14200 E ALAMEDA AVE AURORA, CO 80012',\n",
    "                  'TEL (303) 344-9901']:\n",
    "    if re.search(r'', text_line):\n",
    "        print(text_line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed8a2fea",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aac40614",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">TEL (303) 344-9901</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb41a800",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "716f5ab4",
   "metadata": {},
   "source": [
    "<B>4.4:  <U>Anchors - end of string</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73cb3f07",
   "metadata": {},
   "source": [
    "Print only those files that end in .jpg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5749ec31",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "filenames = ['image.jpg', 'image.png', 'filejpg.txt', 'file2.doc',\n",
    "             'file3.pdf', 'image2.gif', 'image3.jpg', 'image4.jpg']\n",
    "\n",
    "for name in filenames:\n",
    "    if re.search(r'', name):\n",
    "        print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b38c1e87",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64b328ef",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">image.jpg\n",
    "image3.jpg\n",
    "image4.jpg</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d02d009b",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b167d0b",
   "metadata": {},
   "source": [
    "BUILT-IN CHARACTER CLASSES"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2200afb4",
   "metadata": {},
   "source": [
    "<B>4.5:  <U>\"Digit\" character class</U>.   </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e8e40e5",
   "metadata": {},
   "source": [
    "Match on each string that has a digit.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fe35d3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "384039a9",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "823f263d",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello world 00\n",
    " 23 bonjour\n",
    "wilkommen23\n",
    "99\n",
    "00\n",
    "88557799\n",
    "Que 3 Tal!\n",
    "count: 7</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1a3a443",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cc8b842",
   "metadata": {},
   "source": [
    "<B>4.6:  <U>\"Word\" character class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ef49269",
   "metadata": {},
   "source": [
    "Match each string that has a letter, number or underscore.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3a51a116",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d16ab154",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0ce3d54",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\"></DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f6d32d8",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51bb4d15",
   "metadata": {},
   "source": [
    "<B>4.7:  <U>\"Space\" character class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00552cee",
   "metadata": {},
   "source": [
    "Match on each line that has a space.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2338f74d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3cfc63b",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "360a0027",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\"></DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e709ca4f",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68fcab22",
   "metadata": {},
   "source": [
    "INVERSE CHARACTER CLASSES"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c56f388",
   "metadata": {},
   "source": [
    "<B>4.8:  <U>\"Not a digit\" character class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bd3ab6b",
   "metadata": {},
   "source": [
    "Match on each string that has a character that is not a digit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a24dddc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c52e67ec",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14e8bfe3",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello world 00\n",
    "goodbye world\n",
    " 23 bonjour\n",
    "wilkommen23\n",
    "aloha\n",
    "Que 3 Tal!\n",
    "myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 8</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2dc60e7",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7def66d",
   "metadata": {},
   "source": [
    "<B>4.9:  <U>\"Not a space\" character class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a726b55b",
   "metadata": {},
   "source": [
    "Match on each string that has any non-spaces."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c5ae1c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    '    ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6de2ec13",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b89b8841",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello world 00\n",
    "goodbye world\n",
    " 23 bonjour\n",
    "wilkommen23\n",
    "aloha\n",
    "99\n",
    "00\n",
    "88557799\n",
    "Que 3 Tal!\n",
    "myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 11</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d161389",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3eb653c",
   "metadata": {},
   "source": [
    "CUSTOM CHARACTER CLASSES"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af3cd582",
   "metadata": {},
   "source": [
    "<B>4.10:  <U>Custom character class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fba9f301",
   "metadata": {},
   "source": [
    "Match on each string that has a capital letter in it.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b49dfe1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14d60a23",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a8ff89b",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">Que 3 Tal!\n",
    "yourfile.JPG\n",
    "count: 2</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4eb639e1",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1cbabfe",
   "metadata": {},
   "source": [
    "<B>4.11:  <U>Using custom character class with built-in character class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86244f47",
   "metadata": {},
   "source": [
    "Match on each string that has a letter followed by a number.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "124b6692",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd47b705",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50ce22ff",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">wilkommen23\n",
    "count: 1</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffd1998f",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93a74fb5",
   "metadata": {},
   "source": [
    "INVERSE CUSTOM CHARACTER CLASSES"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e02223b9",
   "metadata": {},
   "source": [
    "<B>4.12:  <U>Inverse Custom Character Class</U>. Match on each string that has any character that is not a letter.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1971fe9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ce22f63",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81267e8b",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello world 00\n",
    "goodbye world\n",
    " 23 bonjour\n",
    "wilkommen23\n",
    "99\n",
    "00\n",
    "88557799\n",
    "Que 3 Tal!\n",
    "myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 10</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2959aa2",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fe3a281",
   "metadata": {},
   "source": [
    "THE WILDCARD"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93da549f",
   "metadata": {},
   "source": [
    "<B>4.13:  Match on each string that ends with a character that is not a digit.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "298196b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfd2bbd3",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "449ff799",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">goodbye world\n",
    " 23 bonjour\n",
    "wilkommen23\n",
    "aloha\n",
    "Que 3 Tal!\n",
    "myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 7</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11ba0cf7",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1a65f57",
   "metadata": {},
   "source": [
    "<B>4.14:  <U>Demo:  match on any character</U>. </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9af89afe",
   "metadata": {},
   "source": [
    "Use the wildcard (., a period) to see which strings match it.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5515f3e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84cccbef",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9930b956",
   "metadata": {},
   "source": [
    "LAB 1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd548a2d",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.15:  Match on each string that starts with a digit.   </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "817fcaab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "305b4386",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7337375d",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">99\n",
    "00\n",
    "88557799\n",
    "count: 3</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5844eda",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6c1b098",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.16:  Match on each string that starts with a space.  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb15313d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "lines = [  'this is the first line,',\n",
    "           ' and this is the second line and',\n",
    "           ' this is the third line.  '     ]\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'', line):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebf12103",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d09b3707",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\"> and this is the second line and',\n",
    " this is the third line.  '     ]</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d0c5c73",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f569df72",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.17:  Loop through and print only lines with some text (not including spaces).  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f694944a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = \"\"\"line 1\n",
    "line 2,\n",
    "\n",
    "line 3...\n",
    "\n",
    "line4!\"\"\"\n",
    "\n",
    "lines = text.splitlines()\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'', line):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ef822e3",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "364f28c6",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">line 1\n",
    "line 2,\n",
    "line 3...\n",
    "line4!</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb9ca201",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dab5111a",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.18:  Match on each string that ends with a digit.</B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7876a822",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0becf43",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6cb0875",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello world 00\n",
    "99\n",
    "00\n",
    "88557799\n",
    "count: 4</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06d69d87",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "322c5dec",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.19:  Match on each line that ends with a space.</B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ac0188d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "lines = [  'this is the first line, ',\n",
    "           'this is the second line and',\n",
    "           'this is the third line.  '     ]\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'', line):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "860975c7",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f82b513",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">this is the first line,\n",
    "this is the third line.</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48fa844d",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "175c430f",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.20:  Match on each string that consists only of a 2-digit number.</B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c572f67d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48eeec78",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f470ad0",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">99\n",
    "00\n",
    "count: 2</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3f2cb02",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a0e71d4",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.21:  Match on a capital letter followed by a lowercase letter.  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee8b2cb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ce4ead8",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b58cf25",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">Que 3 Tal!</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d20e3d8",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c00a061",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.22:  Match on files with date format YYYY-MM-DD followed by '.txt'.  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28d841a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "dirlist = ('.', '..', '2010-12-15.txt', '2010-12-16.txt',\n",
    "           'testfile.txt', '20101-11-03.txt')\n",
    "\n",
    "for item in dirlist:\n",
    "    if re.search(r'', item):\n",
    "        print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc001f01",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dea9c76d",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">2010-12-15.txt\n",
    "2010-12-16.txt</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "300fc75a",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99aa5b9c",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.23:  Match on date format MM/DD/YY (and not 4-digit year).  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7be523d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "dates = ['Jan. 3, 2018', '23-Mar-17', '12/02/98', '12/03/1998', '23.17.2018']\n",
    "for date in dates:\n",
    "    if re.search(r'', date):\n",
    "        print(date)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c1e2e09",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0f37ebc",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">12/02/98</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42e2fa48",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbb8fcf7",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.24:  Determine whether selected word begins with a vowel.  If so, prepend an 'an' rather an an 'a'.  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b174f578",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "words = ['apple', 'pear', 'orange', 'kiwi', 'elderberry', 'carrot', 'ugli fruit']\n",
    "for word in words:\n",
    "    if re.search(r'', word):\n",
    "        prepend = 'an'\n",
    "    else:\n",
    "        prepend = 'a'\n",
    "    print(f\"{prepend} {word}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4ef8f08",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e607980",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">an apple\n",
    "a pear\n",
    "an orange\n",
    "a kiwi\n",
    "an elderberry\n",
    "a carrot\n",
    "an ugli fruit</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ba3b8cf",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65c35914",
   "metadata": {},
   "source": [
    "BUILT-IN QUANTIFIERS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81377526",
   "metadata": {},
   "source": [
    "<B>4.25:  <U>\"One or more\" quantifier</U>.  Match on each string that has one or more letters in it.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f32e6504",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "656833f5",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfedd1f0",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello world 00\n",
    "goodbye world\n",
    " 23 bonjour\n",
    "wilkommen23\n",
    "aloha\n",
    "Que 3 Tal!\n",
    "myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 8</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b8f98ae",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7d754b1",
   "metadata": {},
   "source": [
    "<B>4.26:  <U>\"Zero or one\" quantifier</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8d81423",
   "metadata": {},
   "source": [
    "Without using a character class (or grouped alternates), use a single regex that matches on each string that has 'a' or 'an' followed by a space.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f84ed458",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "lines = [\n",
    "    'This is a wonderful thing. ',\n",
    "    \"I haven't seen anything like it. \",\n",
    "    \"Isn't it an exceptional experience? \"]\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'', line):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e7fb7c2",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc5790ac",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">This is a wonderful thing.\n",
    "Isn't it an exceptional experience?</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41dd164f",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e037be9f",
   "metadata": {},
   "source": [
    "<B>4.27:  <U>\"Zero or more\" quantifier, quantifiers with anchor</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3a0ebe0",
   "metadata": {},
   "source": [
    "Match on all strings that consist only of a 1 followed by zero or more digits.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7e78965",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "numbers = [\n",
    "  '100',\n",
    "  '135',\n",
    "  '31',\n",
    "  '1',\n",
    "  'I think',\n",
    "]\n",
    "\n",
    "for val in numbers:\n",
    "    if re.search(r'', val):\n",
    "        print(val)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78618ad7",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4754d564",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">100\n",
    "135\n",
    "1</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d5d89a0",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00bce761",
   "metadata": {},
   "source": [
    "<B>4.28:  <U>Quantifiers with Anchor</U>.  Match on each string that consists only of one or more digit characters.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "708f9b10",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e6dc8e8",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15fd2fa5",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">99\n",
    "00\n",
    "88557799\n",
    "count: 3</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ae70361",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c21e9461",
   "metadata": {},
   "source": [
    "<B>4.29:  <U>Quantifiers with Anchor (3)</U>.  Match on each string that consists only of letters.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "440f5990",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c512e0cd",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b25de6eb",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">aloha\n",
    "count: 1</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6bd4bbdf",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a2d35a2",
   "metadata": {},
   "source": [
    "<B>4.30:  <U>Quantifiers with custom character class</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d1d44e5",
   "metadata": {},
   "source": [
    "Match each string that has a capital letter followed by one or more lowercase letters.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e9e1157",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello World 00',\n",
    "    'goodbye C world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'Aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1015078d",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec414eb7",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello World 00\n",
    "Aloha\n",
    "Que 3 Tal!\n",
    "count: 3</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbf080d7",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a670aa6",
   "metadata": {},
   "source": [
    "<B>4.31:  <U>Quantifiers with anchors</U>.  Match on each string that consists only of letters, numbers or the underscore.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c37d8ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb3f3210",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9d57596",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">aloha\n",
    "99\n",
    "00\n",
    "88557799\n",
    "count: 4</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fad46f70",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24c1fe6a",
   "metadata": {},
   "source": [
    "<B>4.32:  <U>Quantifiers with anchors (2)</U>.  Match on each string that consists only of non-digits.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "300504a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a637581",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "724d8032",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">goodbye world\n",
    "aloha\n",
    "myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 4</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "596d854a",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4f12e88",
   "metadata": {},
   "source": [
    "<B>4.33:  <U>Quantifiers with anchors (3)</U>.  Match on each string that consists only of non-spaces.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f408a3d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e8e64e2",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "001e8008",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">aloha\n",
    "99\n",
    "00\n",
    "88557799\n",
    "myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 6</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b32406ba",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d417e0bd",
   "metadata": {},
   "source": [
    "CUSTOM QUANTIFIERS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "150030f9",
   "metadata": {},
   "source": [
    "<B>4.34:  <U>Custom quantifier</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7dcc326",
   "metadata": {},
   "source": [
    "Match on each string that has two or more spaces at the end.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d566f02a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a58f7b0",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94c8fc9a",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">goodbye world\n",
    "wilkommen23\n",
    "count: 2</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5cba88a",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3866ca7e",
   "metadata": {},
   "source": [
    "<B>4.35:  <U>Custom quantifier</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5a7520d",
   "metadata": {},
   "source": [
    "Match on strings that have a capital letter followed by two or more lowercase letters.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "464a9d02",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello World 00',\n",
    "    'goodbye As world    ',\n",
    "    'To 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'Aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f87593c3",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efd12641",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello World 00\n",
    "Aloha\n",
    "Que 3 Tal!\n",
    "count: 3</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06c8b0e2",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31537290",
   "metadata": {},
   "source": [
    "<B>4.36:  <U>Custom quantifier</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87508aa9",
   "metadata": {},
   "source": [
    "Print those numbers that are in the millions (i.e., 7 or more digits)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "637f378e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "nums = [\n",
    "  '1',\n",
    "  '10',\n",
    "  '100',\n",
    "  '1000',\n",
    "  '10000',\n",
    "  '100000',\n",
    "  '1000000',\n",
    "  '10000000'\n",
    "]\n",
    "\n",
    "for num in nums:\n",
    "    if re.search(r'', num):\n",
    "        print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bee722f6",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0627cfeb",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">1000000\n",
    "10000000</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d22b18e7",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a66b47b",
   "metadata": {},
   "source": [
    "<B>4.37:  <U>Custom quantifier</U>.  Having split the text into words, show those words that are greater than 7 characters in size.  </B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5eefbdf5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = \"\"\"This is the 1000th story, regarding a duck\n",
    "named Quack.  It was unlikely that Quack could have been\n",
    "given a name like that by his mother, so we can only conclude\n",
    "that he was named by the author, who has a cuteness problem.\"\"\"\n",
    "\n",
    "\n",
    "words = text.split()\n",
    "stripped = [ word.rstrip('.,') for word in words ]\n",
    "\n",
    "for word in stripped:\n",
    "    if re.search(r'', word):\n",
    "        print(word)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d60faa5c",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bcdeae0",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">regarding\n",
    "unlikely\n",
    "conclude\n",
    "cuteness\n",
    "problem</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9976a56b",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bee23d3",
   "metadata": {},
   "source": [
    "<B>4.38:  <U>Custom Quantifier</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54a261d8",
   "metadata": {},
   "source": [
    "A password must be 3-8 characters in length (letters, numbers and underscores are permitted).  Validate the below password attempts.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85918eb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "attempts = [\n",
    "    '1234',\n",
    "    'hello_there',\n",
    "    'password',\n",
    "    'ok',\n",
    "    'what?',\n",
    "    'supercalifrag']\n",
    "\n",
    "for password in attempts:\n",
    "    if re.search(r'', password):\n",
    "        print(f'{password}:  validated')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7de7785b",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d53a2be1",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">1234:  validated\n",
    "password:  validated</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59426934",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4785f7a",
   "metadata": {},
   "source": [
    "ESCAPING SPECIAL CHARACTERS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09a84616",
   "metadata": {},
   "source": [
    "<B>4.39:  <U>Escape wildcard (aka period)</U>.  Match on each string that has a letter, number or underscore followed by a period.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c037307e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13ba8f1a",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa824f8f",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 2</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68399e79",
   "metadata": {},
   "source": [
    "Note:  why would this work without escaping the period?  Because"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55872f4c",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e29c2f10",
   "metadata": {},
   "source": [
    "<B>4.40:  <U>Escape end anchor (aka dollar sign)</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8a5ae1f",
   "metadata": {},
   "source": [
    "Match on strings that have a dollar amount, including two decimal places ($23.53).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52a1e1c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "lines = [\n",
    "    'The coat cost $239.50.',\n",
    "    'The candy cost $1.93',\n",
    "    \"I didn't buy anything today.\",\n",
    "    '$1 sale',\n",
    "    'I dream of $$$'\n",
    "]\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'', line):\n",
    "        print(line)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a15e1caa",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f0e6e30",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">The coat cost $239.50.\n",
    "The candy cost $1.93</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83095322",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7608a14",
   "metadata": {},
   "source": [
    "<B>4.41:  <U>Escape quantifier character +</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28e05d40",
   "metadata": {},
   "source": [
    "Match on all lines with positive numbers.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "299dcb88",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "numbers = [\n",
    "    'Amount:  -23.9',\n",
    "    'Amount:  +43.8',\n",
    "    'Amount:  -9.03',\n",
    "    'Amount:  +99.9',\n",
    "    'Amount:  +22.0'\n",
    "]\n",
    "for num in numbers:\n",
    "    if re.search(r'', num):\n",
    "        print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f351e2f7",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98cb9886",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">Amount:  +43.8\n",
    "Amount:  +99.9\n",
    "Amount:  +22.0</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "601f63c0",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d277624",
   "metadata": {},
   "source": [
    "<B>4.42:  <U>Escape quantifier character *</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6a2fc27",
   "metadata": {},
   "source": [
    "Match on all lines with asterisked footnotes.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "092fb734",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "numbers = [\n",
    "    'As Ibid* said,',\n",
    "    'there should be no greater good ',\n",
    "    'than compassion*, love, ',\n",
    "    'mutual benefit*',\n",
    "    'and the profit-making motive.',\n",
    "]\n",
    "for num in numbers:\n",
    "    if re.search(r'', num):\n",
    "        print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc9a0a82",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "119d7ab3",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">As Ibid* said,\n",
    "than compasssion*, love,\n",
    "mutual benefit*</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "033912ce",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1c065af",
   "metadata": {},
   "source": [
    "LAB 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2037c641",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.43:  Match on each string that has one or more \"word\" characters, followed by one or more spaces, followed by one or more \"word\" characters.</B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7840b471",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8769e2d8",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d15c0b3e",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">hello world 00\n",
    "goodbye world\n",
    " 23 bonjour\n",
    "Que 3 Tal!\n",
    "count: 4</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61cc70fa",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cfc5b4e",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.44:  Ignore comment lines:  print only those lines that don't start with a comment (the first non-space character is a hash mark).  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf4523c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = \"\"\"\n",
    "# this is a program to do stuff\n",
    "a = 5\n",
    "b = 10              # an int\n",
    "if True:\n",
    "    # multiply them\n",
    "    c = a * b\n",
    "\"\"\"\n",
    "\n",
    "for line in text.splitlines():\n",
    "    if not re.search(r'', line):\n",
    "        print(line)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6445be27",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7defb1fb",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">a = 5\n",
    "b = 10              # an int\n",
    "if True:\n",
    "    c = a * b</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43b12019",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0771366",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.45:  Match those lines that contain a 7-digit hex number (a-fA-F0-9).  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e5f21c53",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "lines = [\n",
    "    'The color code is #ABF2307.',\n",
    "    'Mr. Mxyzptlk is 999 years old today.',\n",
    "    'The memory address is fc9d223.'\n",
    "]\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'', line):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38b09281",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8648b747",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">The color code is #ABF2307.\n",
    "The memory address is fc9d223.</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6803f433",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b1200d3",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.46:  Show those lines that contain two capitalized words (as in a name).  </B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e22f9078",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "lines = [\n",
    "    'The owner is Gwen Harstridge.',\n",
    "    \"There aren't a lot of stores like this one.\",\n",
    "    'Paris is not a lot like Rome.',\n",
    "    'I hail from Los Angeles, California.'\n",
    "]\n",
    "\n",
    "for line in lines:\n",
    "    if re.search(r'', line):\n",
    "        print(line)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd40006a",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae798246",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">    The owner is Gwen Harstridge.\n",
    "    I hail from Los Angeles, California.</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1589a067",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8ac9e19",
   "metadata": {},
   "source": [
    "re.IGNORECASE"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcf3718b",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67ddd1f9",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.47:  Without using a character class, match on each string that ends in .jpg or .JPG (try this another way).</B></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ca74176",
   "metadata": {},
   "source": [
    "(hint: use the flag argument (the optional 3rd argument) to re.search())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2dd36488",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4eb94003",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "64354986",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 2</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b738f1ad",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e2b9c00",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.48:  Print only those files that start with 'image#' ('image' plus a <I>possible</I> number) and end in any of these image extensions:  '.jpg', '.png', '.gif'</B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a9f4fc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "filenames = ['image2.jpg', 'image.png', 'file.txt', 'file2.doc',\n",
    "             'file3.pdf', 'image2.gif', 'image3.jpg', 'image4.jpg',\n",
    "             'advert.jpg', 'advert.png']\n",
    "\n",
    "for name in filenames:\n",
    "    if re.search(r'', name):\n",
    "        print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3bb258b",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e848f5e",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">image2.jpg\n",
    "image.png\n",
    "image2.gif\n",
    "image3.jpg\n",
    "image4.jpg</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f904883",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c64db811",
   "metadata": {},
   "source": [
    "<div style=\"background-color: #ccffcc; padding: 10px; border: 2px solid green\"><B>4.49:  Match on each string that ends in .jpg or .JPG</B></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3bc8a9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "match_strings = [\n",
    "    'hello world 00',\n",
    "    'goodbye world    ',\n",
    "    ' 23 bonjour',\n",
    "    'wilkommen23  ',\n",
    "    'aloha',\n",
    "    '99',\n",
    "    '00',\n",
    "    '88557799',\n",
    "    'Que 3 Tal!',\n",
    "    'myfile.jpg',\n",
    "    'yourfile.JPG'\n",
    "]\n",
    "\n",
    "count = 0\n",
    "for string in match_strings:\n",
    "    if re.search(r'', string):\n",
    "        print(string)\n",
    "        count += 1\n",
    "print(f'count:  {count}')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1460ebf0",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "630cdf84",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">myfile.jpg\n",
    "yourfile.JPG\n",
    "count: 2</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beb140ea",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91173810",
   "metadata": {},
   "source": [
    "GROUPING FOR QUANTIFYING and ALTERNATES"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75eb4ee7",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46a21350",
   "metadata": {},
   "source": [
    "<B>4.50:  <U>Quantifying a group</U>.  Match on a number with two decimal places and possible thousandths separator (3.95, 3,200.95, etc.)</B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7a34e41",
   "metadata": {},
   "source": [
    "First create a pattern that is 1 or more digits with comma separator (i.e. matching on <B>0,</B>, </B>00,</B> <B>000,</B>) and group the number with parentheses; quantify the group to say that there is zero or more of these, followed by one or more digits, a period and 2 digits.  (Do not use a custom character class for this purpose.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2302b9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "values = ['23.9', '18.2', '23.95', '2,238,000.00', '15,382.92', 'joe', '6.05']  # list of str\n",
    "for value in values:\n",
    "    matchobj = re.search(r'', value)\n",
    "    if matchobj:\n",
    "        print(value)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7995270",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13acc633",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">23.95\n",
    "2,238,000.00\n",
    "15,382.92</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4123c40",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3647c428",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9640e7de",
   "metadata": {},
   "source": [
    "<B>4.51:  <U>Quantifying a Group (2)</U>.  Write a single regex that matches on q, Q, quit, Quit, QUIT.  Do this without a character class and without the alternate vertical bar.  </B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97aef4ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "x = input('Do you want to quit? ')     # str, 'QuIt' (sample input)\n",
    "\n",
    "if re.search(r'', x):\n",
    "    print(\"you're quitting!\")\n",
    "else:\n",
    "    print(\"you failed to quit.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87686e39",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d80e7df",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">Do you want to quit? QuIt\n",
    "you're quitting!</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27b2a129",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98028b30",
   "metadata": {},
   "source": [
    "GROUPING FOR EXTRACTION"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "918ea3c2",
   "metadata": {},
   "source": [
    "<B>4.52:  <U>Group for extraction</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f351834",
   "metadata": {},
   "source": [
    "Use a parenthetical grouping to extract the number from this text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e367412",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "line = '34:  this is a line of text'\n",
    "\n",
    "matchobj = re.search(r'', line)\n",
    "print(matchobj.group(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88cfcff2",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f05e37e",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">34</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33c8ccf9",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5275775",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ff93f6",
   "metadata": {},
   "source": [
    "<B>4.53:  <U>Group for extraction</U>.  Extract the Catalog ID and Publication Date from the text line.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "176dc97e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "rs_row = 'Catalog ID: 2839-587    Pub. Date: 2019-09-03'\n",
    "\n",
    "matchobj = re.search(r'', rs_row)\n",
    "\n",
    "if matchobj:\n",
    "    print(matchobj.group(1))\n",
    "    print(matchobj.group(2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "422afec6",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f7971a9",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">2839-587\n",
    "2019-09-03</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d55e032c",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27499fef",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21a7206f",
   "metadata": {},
   "source": [
    "<B>4.54:  <U>Group for extraction</U>.  In one regex match, extract the IP address from this log line.</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2e906c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "line = '172.26.93.208 - - [28/Jun/2012:21:00:17 -0400] \"GET /~cmk380/pythondata/image2b.txt HTTP/1.1\" 200 30'\n",
    "\n",
    "matchobj = re.search(r'', line)\n",
    "\n",
    "if matchobj:\n",
    "    print(matchobj.group(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "083747ce",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "636d9827",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">172.26.93.208</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bfb4d74a",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf2578a1",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff557057",
   "metadata": {},
   "source": [
    "'MINIMAL MATCH' QUANTIFIER"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f9b866f",
   "metadata": {},
   "source": [
    "<B>4.55:  <U>Demonstration:  \"minimal\" match</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26bdaf77",
   "metadata": {},
   "source": [
    "The below regex grabs the word <B>Python</B> from the text.  Run the code once to observe this.\n",
    "\n",
    "Now add a question mark <B>?</B> as the character directly after the \"one or more\" plus sign and run again - you should see that the \"one or more word characters\" pattern is now matching on as few characters as possible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6eedb3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = 'My language is Python'\n",
    "\n",
    "matchobj = re.search(r'', text)\n",
    "\n",
    "print(matchobj.group(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd8a05f5",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8371975",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">P</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c62370a9",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63065fee",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7b53c3f",
   "metadata": {},
   "source": [
    "<B>4.56:  <U>Work with wildcard and minimal match</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91767c31",
   "metadata": {},
   "source": [
    "Use the wildcard to match everything between the first two brackets.  Note carefully what was printed.  (Don't forget that square brackets must be escaped with a backslash, and that extraction requires grouping parentheses.)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "814b37ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = 'Discussion of terms <TO COME> after something <PLEASE REVIEW>.'\n",
    "\n",
    "matchobj = re.search(r'', text)\n",
    "\n",
    "print(matchobj.group(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3dda0b5c",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f217fe66",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">TO COME</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5b8725e",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcd5338e",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d266bb30",
   "metadata": {},
   "source": [
    "<B>4.57:  <U>Match on non-search character</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "448a9fde",
   "metadata": {},
   "source": [
    "Perform the same extraction on the below text by searching for a bracket followed by one or more non-brackets.  Text extracted should be the same.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44aa6224",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = 'Discussion of terms <TO COME> after something <PLEASE REVIEW>.'\n",
    "\n",
    "matchobj = re.search(r'', text)\n",
    "\n",
    "print(matchobj.group(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b19072d6",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fdafb06",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">TO COME</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2169e9a1",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86e13eda",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6ced52f",
   "metadata": {},
   "source": [
    "GROUPING with .groups()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2fc71a1",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3143df36",
   "metadata": {},
   "source": [
    "<B>4.58:  <U>Retrieve a grouping with <B>.groups()</B></U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3eb58c79",
   "metadata": {},
   "source": [
    "In one regex match, extract the status code and bytes downloaded (last 2 integers on the line) from this log line.  Call <B>.groups()</B> the match object to reveal the extracted values.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c90230f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "line = '172.26.93.208 - - [28/Jun/2012:21:00:17 -0400] \"GET /~cmk380/pythondata/image2b.txt HTTP/1.1\" 200 30'\n",
    "\n",
    "matchobj = re.search(r'', line)\n",
    "\n",
    "if matchobj:\n",
    "    print(matchobj.groups())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4e38f83",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a53f5049",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">('200', '30')</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb79f96f",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89f7249b",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90d96eac",
   "metadata": {},
   "source": [
    "<B>4.59:  <U>Retrieve a grouping with <B>.groups()</B></U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb7fe56d",
   "metadata": {},
   "source": [
    "Extract city, state zip from line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5abac051",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "line = 'Los Angeles, CA  91604'\n",
    "matchobj = re.search(r'', line)\n",
    "print(matchobj.groups())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac1b60bf",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d672f3f",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">('Los Angeles', 'CA', '91604')</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99839fd3",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29f56a78",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa5e7e84",
   "metadata": {},
   "source": [
    "<B>4.60:  <U>Quantify for an optional group</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17d1f218",
   "metadata": {},
   "source": [
    "Pull out all the info about each person (Favorite Color may not be there).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d62a03d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "results = [ 'Name: Joe;  Favorite Color:  Blue;  Employee ID: 2395',\n",
    "            'Name:  Marie;  Employee ID: 2321',\n",
    "            'Name:  Teneski; Favorite Color: Green;  Employee ID:  1913' ]\n",
    "\n",
    "for row in results:\n",
    "    matchobj = re.search(r'', row)\n",
    "    print(matchobj.groups())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c4e8547",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9579370d",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">('Joe', 'Favorite Color:  Blue;  ', 'Blue', '2395')\n",
    "('Marie', None, None, '2321')\n",
    "('Teneski', 'Favorite Color: Green;  ', 'Green', '1913')</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9211cbe",
   "metadata": {},
   "source": [
    "Note that if you see the message <B>AttributeError: 'NoneType' object has no attribute 'group'</B>, this means that the search did not find a match and returned <B>None</B>, and the code attempted to call <B>.group()</B> on <B>None</B>.  Check the string and pattern to determine why it did not match."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1216ac31",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1859955",
   "metadata": {},
   "source": [
    "findall() FOR MULTIPLE MATCHES"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d5847454",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95b0e66a",
   "metadata": {},
   "source": [
    "<B>4.61:  <U>Group and extract with <B>findall()</B></U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a753c8c5",
   "metadata": {},
   "source": [
    "Extract email addresses only for nyu.edu."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d0bce97",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = \"\"\"There are many ways to contact us.  Use the\n",
    "general email contact@nyu.edu, or email our public\n",
    "liason at help@nyu.edu.  If you need tech support you\n",
    "can reach us at askits@nyu.edu.\n",
    "Author:  Joe Wilson joe@wilson.com\"\"\"\n",
    "\n",
    "emails = re.findall(r'', text)\n",
    "print(emails)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5d9770f",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06682ed0",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">['contact@nyu.edu', 'help@nyu.edu', 'askits@nyu.edu']</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91abffb0",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91e9f884",
   "metadata": {},
   "source": [
    "re.sub() FOR SUBSTITUTIONS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "306d2b2e",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35d64eec",
   "metadata": {},
   "source": [
    "<B>4.62:  <U>Regex substitution</U>.  Replace space-separated with comma separated</B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aff70d82",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "args = 'this that   other  and  some  other'\n",
    "args2 = re.sub(r'', \",\", args)\n",
    "print(args2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3115359",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dafd3325",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">this,that,other,and,some,other</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b978476",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c456d2d2",
   "metadata": {},
   "source": [
    "re.split() FOR PATTERN-BASED DELIMITERS"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "008856c6",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f0eef0f",
   "metadata": {},
   "source": [
    "<B>4.63:  <U>Regex split</U>.  Split the user-input comma-separated values string into separate digit values.  </B>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69aa84a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "ui = '23, 14, 7,3,9'\n",
    "numbers = re.split(r'', ui)\n",
    "print(numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2441228",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a07d5853",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">['23', '14', '7', '3', '9']</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20a8949a",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b90ea68a",
   "metadata": {},
   "source": [
    "<B>4.64:  <U>DOTALL wildcard match</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf99c60f",
   "metadata": {},
   "source": [
    "Extract everything between <B>=code start=</B> and <B>= code end =</B>.  Use the <B>re.DOTALL</B> switch to use the wildcard (<B>.</B>) to match on a newline.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a59e1d39",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = \"\"\"Title of This Text\n",
    "\n",
    "This is some description...\n",
    "\n",
    "=code start=\n",
    "a = 5\n",
    "b = 5.0\n",
    "if a == b:\n",
    "    print('yes')\n",
    "=code end=\n",
    "\n",
    "This is some discussion...\n",
    "\"\"\"\n",
    "\n",
    "matchobj = re.search(r'', text)\n",
    "\n",
    "print(matchobj.group(1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d555918",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9bbc1079",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">a = 5\n",
    "b = 5.0\n",
    "if a == b:\n",
    "    print('yes')</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "333ca0f5",
   "metadata": {},
   "source": [
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edfe7ea7",
   "metadata": {},
   "source": [
    "<B>4.65:  <U>Multiline anchors</U>.  </B>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d22f6c36",
   "metadata": {},
   "source": [
    "Use <B>findall()</B> to extra numbers from only the start of each line of the text.  Use <B>re.MULTILINE</B> to allow the carrot (<B>^</B>) to match at the start of any line.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb8b30a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "text = \"\"\"Title of This Text\n",
    "\n",
    "23 we want to grab some 99 numbers\n",
    "12 but not others, 17 and then some\n",
    "5  so we just get 1 the ones\n",
    "on the left side\n",
    "\n",
    "93 and me and 23 too\n",
    "\n",
    "\"\"\"\n",
    "\n",
    "matches = re.findall(r'', text)\n",
    "\n",
    "print(matches)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60066097",
   "metadata": {},
   "source": [
    "<U>Expected Output</U>:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "917a17ac",
   "metadata": {},
   "source": [
    "<pre style=\"margin: 0; padding=0\"><DIV style=\"font-family: Courier; border: 1px solid #dbdbdb; padding: 7px\">['23', '12', '5', '93']</DIV></pre>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f647c255",
   "metadata": {},
   "source": [
    " "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 5
}
