Natural Language v. Regex: The Context wars

Private AI
Feb 3, 2021
Share this post
Sharing to FacebookSharing to LinkedInSharing to XSharing to Email

The past decade has seen AI advance in leaps and bounds, with Deep Learning (DL) enabling many new applications and redefining industries. Computer Vision has had a particularly significant impact: without it, DL computers could not reliably process real-world images and videos with accuracies high enough to enable real-world applications. Natural Language Processing (NLP) is different in that, prior to Deep Learning, relatively effective techniques did exist, enabling applications such as spam filtering.

Enter the humble regular expression (regex), a method used to look for patterns within text. Regexes have formed an integral part of computer software for decades, especially in Named Entity Recognition (NER) systems. NER applications include gathering keywords for search indexing, product intelligence, and identifying sensitive information.

Say you’re looking for credit card numbers. It’s quite easy to set up a regex that looks for 16-digit numbers or four groups of four numbers separated by a ‘-’. A regex like this is highly effective in the perfect world of computer data, but unfortunately the real world is much more complicated. Let’s take phone numbers. Numbers come in different lengths and arrangements, and change based on where the caller is located.

The following numbers are all equivalent, for instance:

00049 153 43437 800 – Extra 0 to dial out of an office phone system

+49 153 434 37 800 – ‘+’ international dialing code format

01534 347800 – ‘Standard’ infra country format

15 343 478 00 – People often drop the leading digit from their locale

Phone numbers can also contain letters. For example, people frequently write their internal office number just using their extension, typically starting with ’x’; e.g., ‘x51399’. Hotlines are another good example, often using the letters associated with each number to make the number easier to remember. For example, a taxi hotline in Australia writes ‘132227’ as ‘13taxi’.

Certain numbers also have different meanings. For example, ‘911’ could refer to the Porsche 911 or the 9/11 attacks. Replacement part numbers are another good example.

Google actually built a system for finding phone numbers using regular expressions

The system was manually programmed to find numbers and check whether they’re valid for each country in the world. The system works well for regular numbers and is even able to catch the above example with an extra 0 for office systems. It is not, however, able to find alphanumeric numbers such as ‘x5177’ or ‘13taxi’.

It isn’t feasible to program a regex for every single possible pattern

In addition to the ludicrous amount of developer time it would take to program, optimize, and maintain this fictional ‘perfect regex’, the unfortunate truth is that it would also return so many false matches that the output would no longer be useful. Regex-based solutions also require a lot of work to maintain, as the expressions constantly need to be tweaked to account for new patterns and false matches. This task coincidentally happens to be one of the least favourite developer chores out there.

Natural Language v. Regex: The Context Wars

Today’s highly connected world requires international solutions dealing with regional differences in standardization

For example, a German address could be ‘Eugen-Schoenhaar-strasse 21, 10423 Berlin’. The house number is written after the street name, whilst ‘strasse’ (street in German) is joined to the street name in one long compound word, because Germans love those. Postal codes are another good example. Dutch and Canadian postal codes are made up of 6 digits. A Dutch postal code however is 4 digits followed by 2 letters (E.g. 1234AY), whereas a Canadian postal code is a mix of digits and letters (E.g. 1A32Y4). An Australian postal code on the other hand is only 4 digits.

Building a system that performs reliably and delivers a level of accuracy high enough for production applications requires handling each of these variants and edge cases, such as phone numbers containing letters. And that credit card number example? Well, it turns out not all credit cards are 16 digits long!

AI models understand context

Humans can easily distinguish between the above examples to determine what is or is not a phone number, an address, etc. We do this by looking at the number, but also the context (e.g. “call 911!” and “gosh that’s a nice 911”). Unlike regexes, AI models understand context and are therefore able to understand text more like humans do.

State-of-the-art AI systems (like our models at Private AI) are trained on large amounts of carefully annotated data and meticulously revised to account for all these edge cases and locale-specific differences. In Private AI’s case, we reach >97% in-domain accuracy, which we have found to be higher than human performance in most settings.

Regex systems require careful analysis of existing expressions

AI systems also scale better and are easier to maintain. Integrating a change to a regex-based system requires careful analysis of the existing expressions, which become more and more difficult to comprehend as one adds to them, to make sure that any changes don’t affect existing expressions and adequately capture all the possible permutations of a term. It is common to ‘fix one thing, break another’ and it often requires a few iterations in production before a change is successfully made. On the other hand, AI systems only require some extra training data.

Minimal training data is needed

In the past, one drawback of AI systems was the need for a large amount of data to train them. However, modern techniques allow AI systems to be developed with just a fraction of the training data that was previously required. For example, Private AI’s solution can learn to generalize from as few as 10 examples.

AI-based systems were also held back by the tremendous amount of computing power they required – far more than regex-based systems. This resulted in large cloud bills and difficulty integrating models into edge applications such as mobile apps or desktop applications. AI is also rapidly advancing in this area and the latest techniques enable large reductions in compute resources. At Private AI, we have spent a large amount of time optimizing our solution to the point where it is now 25x faster than BERT large, a popular NLP architecture, whilst also surpassing its performance.

New levels of performance

Regexes have served as an integral part of computing systems for decades, and will continue to do so. AI-based systems, however, offer new levels of performance by understanding context similarly to humans. For unstructured real-world applications like detecting phone numbers in text, these new techniques enable a wide range of new, high-quality production applications.

Interested in seeing an AI system in the works? Connect with us to test out a free live demo.

Data Left Behind: AI Scribes’ Promises in Healthcare

Data Left Behind: Healthcare’s Untapped Goldmine

The Future of Health Data: How New Tech is Changing the Game

Why is linguistics essential when dealing with healthcare data?

Why Health Data Strategies Fail Before They Start

Private AI to Redefine Enterprise Data Privacy and Compliance with NVIDIA

EDPB’s Pseudonymization Guideline and the Challenge of Unstructured Data

HHS’ proposed HIPAA Amendment to Strengthen Cybersecurity in Healthcare and how Private AI can Support Compliance

Japan's Health Data Anonymization Act: Enabling Large-Scale Health Research

What the International AI Safety Report 2025 has to say about Privacy Risks from General Purpose AI

Private AI 4.0: Your Data’s Potential, Protected and Unlocked

How Private AI Facilitates GDPR Compliance for AI Models: Insights from the EDPB's Latest Opinion

Navigating the New Frontier of Data Privacy: Protecting Confidential Company Information in the Age of AI

Belgium’s Data Protection Authority on the Interplay of the EU AI Act and the GDPR

Enhancing Compliance with US Privacy Regulations for the Insurance Industry Using Private AI

Navigating Compliance with Quebec’s Act Respecting Health and Social Services Information Through Private AI’s De-identification Technology

Unlocking New Levels of Accuracy in Privacy-Preserving AI with Co-Reference Resolution

Strengthened Data Protection Enforcement on the Horizon in Japan

How Private AI Can Help to Comply with Thailand's PDPA

How Private AI Can Help Financial Institutions Comply with OSFI Guidelines

The American Privacy Rights Act – The Next Generation of Privacy Laws

How Private AI Can Help with Compliance under China’s Personal Information Protection Law (PIPL)

PII Redaction for Reviews Data: Ensuring Privacy Compliance when Using Review APIs

Independent Review Certifies Private AI’s PII Identification Model as Secure and Reliable

To Use or Not to Use AI: A Delicate Balance Between Productivity and Privacy

To Use or Not to Use AI: A Delicate Balance Between Productivity and Privacy

News from NIST: Dioptra, AI Risk Management Framework (AI RMF) Generative AI Profile, and How PII Identification and Redaction can Support Suggested Best Practices

Handling Personal Information by Financial Institutions in Japan – The Strict Requirements of the FSA Guidelines

日本における金融機関の個人情報の取り扱い - 金融庁ガイドラインの要件

Leveraging Private AI to Meet the EDPB’s AI Audit Checklist for GDPR-Compliant AI Systems

Who is Responsible for Protecting PII?

How Private AI can help the Public Sector to Comply with the Strengthening Cyber Security and Building Trust in the Public Sector Act, 2024

A Comparison of the Approaches to Generative AI in Japan and China

Updated OECD AI Principles to keep up with novel and increased risks from general purpose and generative AI

Is Consent Required for Processing Personal Data via LLMs?

The evolving landscape of data privacy legislation in healthcare in Germany

The CIO’s and CISO’s Guide for Proactive Reporting and DLP with Private AI and Elastic

The Evolving Landscape of Health Data Protection Laws in the United States

Comparing Privacy and Safety Concerns Around Llama 2, GPT4, and Gemini

How to Safely Redact PII from Segment Events using Destination Insert Functions and Private AI API

WHO’s AI Ethics and Governance Guidance for Large Multi-Modal Models operating in the Health Sector – Data Protection Considerations

How to Protect Confidential Corporate Information in the ChatGPT Era

Unlocking the Power of Retrieval Augmented Generation with Added Privacy: A Comprehensive Guide

Leveraging ChatGPT and other AI Tools for Legal Services

Leveraging ChatGPT and other AI tools for HR

Leveraging ChatGPT in the Banking Industry

Law 25 and Data Transfers Outside of Quebec

The Colorado and Connecticut Data Privacy Acts

Unlocking Compliance with the Japanese Data Privacy Act (APPI) using Private AI

Tokenization and Its Benefits for Data Protection

Private AI Launches Cloud API to Streamline Data Privacy

Processing of Special Categories of Data in Germany

End-to-end Privacy Management

Privacy Breach Reporting Requirements under Law25

Migrating Your Privacy Workflows from Amazon Comprehend to Private AI

A Comparison of the Approaches to Generative AI in the US and EU

Benefits of AI in Healthcare and Data Sources (Part 1)

Privacy Attacks against Data and AI Models (Part 3)

Risks of Noncompliance and Challenges around Privacy-Preserving Techniques (Part 2)

Enhancing Data Lake Security: A Guide to PII Scanning in S3 buckets

The Costs of a Data Breach in the Healthcare Sector and its Privacy Compliance Implications

Navigating GDPR Compliance in the Life Cycle of LLM-Based Solutions

What’s New in Version 3.8

How to Protect Your Business from Data Leaks: Lessons from Toyota and the Department of Home Affairs

New York's Acceptable Use of AI Policy: A Focus on Privacy Obligations

Safeguarding Personal Data in Sentiment Analysis: A Guide to PII Anonymization

Changes to South Korea’s Personal Information Protection Act to Take Effect on March 15, 2024

Australia’s Plan to Regulate High-Risk AI

How Private AI can help comply with the EU AI Act

Comment la Loi 25 Impacte l'Utilisation de ChatGPT et de l'IA en Général

Endgültiger Entwurf des Gesetzes über Künstliche Intelligenz – Datenschutzpflichten der KI-Modelle mit Allgemeinem Verwendungszweck

How Law25 Impacts the Use of ChatGPT and AI in General

Is Salesforce Law25 Compliant?

Creating De-Identified Embeddings

Exciting Updates in 3.7

EU AI Act Final Draft – Obligations of General-Purpose AI Systems relating to Data Privacy

FTC Privacy Enforcement Actions Against AI Companies

The CCPA, CPRA, and California's Evolving Data Protection Landscape

HIPAA Compliance – Expert Determination Aided by Private AI

Private AI Software As a Service Agreement

EU's Review of Canada's Data Protection Adequacy: Implications for Ongoing Privacy Reform

Acceptable Use Policy

ISO/IEC 42001: A New Standard for Ethical and Responsible AI Management

Reviewing OpenAI's 31st Jan 2024 Privacy and Business Terms Updates

Comparing OpenAI vs. Azure OpenAI Services

Quebec’s Draft Regulation Respecting the Anonymization of Personal Information

Version 3.6 Release: Enhanced Streaming, Auto Model Selection, and More in Our Data Privacy Platform

Brazil's LGPD: Anonymization, Pseudonymization, and Access Requests

LGPD do Brasil: Anonimização, Pseudonimização e Solicitações de Acesso à Informação

Canada’s Principles for Responsible, Trustworthy and Privacy-Protective Generative AI Technologies and How to Comply Using Private AI

Private AI Named One of The Most Innovative RegTech Companies by RegTech100

Data Integrity, Data Security, and the New NIST Cybersecurity Framework

Safeguarding Privacy with Commercial LLMs

Cybersecurity in the Public Sector: Protecting Vital Services

Privacy Impact Assessment (PIA) Requirements under Law25

Elevate Your Experience with Version 3.5

Fine-Tuning LLMs with a Focus on Privacy

GDPR in Germany: Challenges of German Data Privacy (Part 2)

Comply with US Executive Order on Safe, Secure, and Trustworthy Artificial Intelligence using Private AI

How to Comply with EU AI Act using PrivateGPT