Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1 — Regular Expressions for NLP

Lesson 11 of 39 in the free Natural Language Processing notes on Siksha Sarovar, written by Rohit Jangra.

Regular Expressions for NLP

Regular expressions (regex) are patterns used to match, search, extract, and substitute text — an indispensable tool throughout the NLP preprocessing pipeline (custom tokenizers, cleaning, entity extraction).

Python's re Module — Core Functions

FunctionPurpose
re.match()Match only at the start of the string
re.search()Find the first match anywhere in the string
re.findall()Return all non-overlapping matches as a list
re.sub()Substitute matches with replacement text
re.split()Split a string wherever the pattern matches
import re

text = "Contact us at support@sikshasarovar.com or admin@sikshasarovar.com"

# findall -- extract all email addresses
emails = re.findall(r'[\w.-]+@[\w.-]+\.\w+', text)
print(emails)
# ['support@sikshasarovar.com', 'admin@sikshasarovar.com']

Common Regex Metacharacters

SymbolMeaningExample
.Any character except newlinea.c matches "abc", "axc"
\dAny digit (0-9)\d+ matches "2026"
\wAny word character (alphanumeric + _)\w+ matches "hello_1"
\sAny whitespace\s+ matches " "
*0 or more of the preceding tokenab* matches "a", "ab", "abbb"
+1 or moreab+ matches "ab", "abbb" (not "a")
?0 or 1 (optional)colou?r matches "color", "colour"
^Start of string^Hi matches only if string starts with "Hi"
$End of stringbye$ matches only if string ends with "bye"
[]Character class[aeiou] matches any vowel
{}Exact repetition count\d{10} matches exactly 10 digits
`\`Alternation (OR)`cat\dog` matches "cat" or "dog"

Practical NLP Regex Examples

import re

text = "Call me at 9876543210 or 011-23456789. Meeting on 05/08/2026."

# 1. Extract 10-digit phone numbers
phones = re.findall(r'\b\d{10}\b', text)
print(phones)  # ['9876543210']

# 2. Extract dates in DD/MM/YYYY format
dates = re.findall(r'\d{2}/\d{2}/\d{4}', text)
print(dates)  # ['05/08/2026']

# 3. Remove all digits
no_digits = re.sub(r'\d+', '', text)
print(no_digits)

# 4. Split text on multiple delimiters (comma, semicolon, period)
parts = re.split(r'[.,;]\s*', "NLP is fun, powerful; and useful.")
print(parts)  # ['NLP is fun', 'powerful', 'and useful', '']

Building a Custom Tokenizer with Regex

import re

def custom_tokenize(text):
    # keep words, and standalone punctuation, drop everything else
    return re.findall(r"[A-Za-z]+(?:'[A-Za-z]+)?|[.,!?;]", text)

print(custom_tokenize("Isn't NLP amazing? Yes, it is!"))
# ["Isn't", 'NLP', 'amazing', '?', 'Yes', ',', 'it', 'is', '!']

Cleaning Social Media Text with Regex

import re

tweet = "OMG this movie is amazing!!! 😍 #MustWatch @filmcritic https://example.com/review"

no_urls = re.sub(r'http\S+', '', tweet)
no_mentions = re.sub(r'@\w+', '', no_urls)
no_hashtag_symbol = re.sub(r'#(\w+)', r'\1', no_mentions)   # keep the word, drop '#'
cleaned = re.sub(r'\s+', ' ', no_hashtag_symbol).strip()
print(cleaned)
# OMG this movie is amazing!!! 😍 MustWatch

Regular expressions give you fine-grained, rule-based control that complements the statistical/ML-based methods covered in later units — many real-world NLP pipelines still rely on regex for cleaning and rule-based extraction.