{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2da95378",
   "metadata": {},
   "source": [
    "# Regex Match\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/langchain-ai/langchain/blob/master/docs/docs/guides/evaluation/string/regex_match.ipynb)\n",
    "\n",
    "To evaluate chain or runnable string predictions against a custom regex, you can use the `regex_match` evaluator."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0de44d01-1fea-4701-b941-c4fb74e521e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.evaluation import RegexMatchStringEvaluator\n",
    "\n",
    "evaluator = RegexMatchStringEvaluator()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe3baf5f-bfee-4745-bcd6-1a9b422ed46f",
   "metadata": {},
   "source": [
    "Alternatively via the loader:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f6790c46",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.evaluation import load_evaluator\n",
    "\n",
    "evaluator = load_evaluator(\"regex_match\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "49ad9139",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'score': 1}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check for the presence of a YYYY-MM-DD string.\n",
    "evaluator.evaluate_strings(\n",
    "    prediction=\"The delivery will be made on 2024-01-05\",\n",
    "    reference=\".*\\\\b\\\\d{4}-\\\\d{2}-\\\\d{2}\\\\b.*\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1f5e82a3-247e-45a8-85fc-6af53bf7ff82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'score': 0}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check for the presence of a MM-DD-YYYY string.\n",
    "evaluator.evaluate_strings(\n",
    "    prediction=\"The delivery will be made on 2024-01-05\",\n",
    "    reference=\".*\\\\b\\\\d{2}-\\\\d{2}-\\\\d{4}\\\\b.*\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "168fcd92-dffb-4345-b097-02d0fedf52fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'score': 1}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check for the presence of a MM-DD-YYYY string.\n",
    "evaluator.evaluate_strings(\n",
    "    prediction=\"The delivery will be made on 01-05-2024\",\n",
    "    reference=\".*\\\\b\\\\d{2}-\\\\d{2}-\\\\d{4}\\\\b.*\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d82dab5-6a49-4fe7-b3fb-8bcfb27d26e0",
   "metadata": {},
   "source": [
    "## Match against multiple patterns\n",
    "\n",
    "To match against multiple patterns, use a regex union \"|\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b87b915e-b7c2-476b-a452-99688a22293a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'score': 1}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Check for the presence of a MM-DD-YYYY string or YYYY-MM-DD\n",
    "evaluator.evaluate_strings(\n",
    "    prediction=\"The delivery will be made on 01-05-2024\",\n",
    "    reference=\"|\".join(\n",
    "        [\".*\\\\b\\\\d{4}-\\\\d{2}-\\\\d{2}\\\\b.*\", \".*\\\\b\\\\d{2}-\\\\d{2}-\\\\d{4}\\\\b.*\"]\n",
    "    ),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8ed1f12-09a6-4e90-a69d-c8df525ff293",
   "metadata": {},
   "source": [
    "## Configure the RegexMatchStringEvaluator\n",
    "\n",
    "You can specify any regex flags to use when matching."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0c079864-0175-4d06-9d3f-a0e51dd3977c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "evaluator = RegexMatchStringEvaluator(flags=re.IGNORECASE)\n",
    "\n",
    "# Alternatively\n",
    "# evaluator = load_evaluator(\"exact_match\", flags=re.IGNORECASE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a8dfb900-14f3-4a1f-8736-dd1d86a1264c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'score': 1}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluator.evaluate_strings(\n",
    "    prediction=\"I LOVE testing\",\n",
    "    reference=\"I love testing\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82de8d3e-c829-440e-a582-3fb70cecad3b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}