Sitemap
Sonal Prasad

Sonal Prasad

Regular Expressions

5 min readFeb 17, 2026

--

How were chatbots implemented before transformers came about?

Things You’ll Learn

  • What regular expressions are, and why they matter in early NLP
  • Pattern matching
  • How early chatbots, such as ELIZA, used rules and patterns
  • Where regex still shows up today

Regular Expressions

The very early premise of natural language processing was built upon regular expressions which allow for pattern matching. Regular expressions help you find patterns in text as opposed to finding exact keywords. Here are some examples of how to construct a regular expression:

Press enter or click to view image in full size
Press enter or click to view image in full size
The above images are credited to Speech and Language Processing by Jurafsky.

The following are some examples of regular expressions:

1. You may want to extract the date from a document:

  • \d{2}-\d{2}-\d{4}
  • In the regular expression (regex) above, it looks for 2 digits (digits are referenced by “\d”), a dash, 2 digits, a dash, and then 4 digits.

2. You may look for an email address in a document:

  • ^[a-zA-Z0–9._%+-]+@[a-zA-Z0–9.-]+\.[a-zA-Z]{2,}$
  • In the regex above, it is essentially as such: ____@___. There is an @ sign and there is a period before the last set of characters. The underlined regions refer to some or all of the following: letters, numbers, or special characters.

3. You may search for a dollar value in a document:

  • ^\$?\d+(\.\d{2})?$
  • In the regex above, there is first a dollar sign ‘$’, then some digits, and then optionally afterward there can be a period and digits to indicate cents after the dollar value.

Chatbots

In addition to regular expressions being used to search for strings with a certain pattern in a document, the chatbots from the early days also relied on regular expressions. Pattern matching can be used to recognize phrases from the user’s messages, and then provide relevant outputs.

In 1966, Weizenbaum created a chatbot called ELIZA that mimicked the responses of the psychologist Carl Rogers. The Rogerian style of mental health therapy involves reflecting the patient’s statements back at them. ELIZA can recognize a phrase like “I need X” and output “What would it mean to you if you got X?” ELIZA relies on rules, that are showcased below.

User Message 1:

You hate me

Get Sonal Prasad’s stories in your inbox

Join Medium for free to get updates from this writer.

Rule:

Press enter or click to view image in full size

ELIZA’s Output:

Since ELIZA reflects dialogue back to the user, it takes the word that the user entered. “hate,” and reflects it back at the user. Note: Since ELIZA copies the words that the user says, the chatbot can reply with typos if that’s how the user typed.

User Message 2:

Rule:

ELIZA’s Output:

Press enter or click to view image in full size

In the above example, ELIZA copies the phrase “know everybody laughed at” and substitutes it into the output response.

The above examples of user messages, rules, and outputs are taken from Speech and Language Processing by Jurafsky.

“An input sentence is scanned from left to right. Each word is looked up in a dictionary of keywords. If a word is identified as a keyword, then (apart from the issue of precedence of keywords) only decomposition rules containing that keyword need to be tried.” [1]

Regular Expression Substitution

In addition to regular expressions helping extract strings with a certain pattern, they also can manipulate or substitute strings based on rules. That is what ELIZA does in order to provide a response.

Here is a full conversation between a user and ELIZA.

Press enter or click to view image in full size
The above image is credited to Speech and Language Processing by Jurafsky.

ELIZA uses substitution to replace “I” with “you” or “my” with “your.” ELIZA repeats back to the user what the user is saying. The substitutions also involve ELIZA replacing the user’s message with another string based on a rule. Here are some examples.

Press enter or click to view image in full size
The above image is credited to Speech and Language Processing by Jurafsky.

In the above example, we can take a look at the first line. The user’s message is “He says I’m depressed much of the time.” Since the keyword is “depressed”, we go to the first rule in the above image, and we construct an output message by writing “I am sorry to hear you are depressed.”

How Are Regular Expressions Used Today?

Regular expressions are used today for guardrails and monitoring, such as identifying intense mental health situations. Regex can filter sensitive information, such as Personal Identifiable Information (e.g. credit card number, social security number). Regex can identify whether a topic outside of its scope is being discussed, such as a crisis, and indicate to the user to talk with healthcare professionals. Amazon Bedrock has guardrails that rely on regex to identify, block, or anonymize sensitive data.

Regular expressions can also be used for text processing, such as removing the formatting containing asterisks in a ChatGPT response, data validation, such as when making sure a user is registering an account with their username and password correctly, and for web scraping.

Even with the advent of Large Language Models, regular expressions are still being used regularly in the modern day. Regex was the basis for chatbots historically, but now we have shifted to more advanced architecture for chatbots and agents.

Bibliography

[1] https://courses.cs.umbc.edu/undergraduate/CMSC471/papers/weizenbaum.eliza.1966.pdf

--

--