Day 6: 30 days of Natural Language Processing Series with Projects
Regular Expressions in NLP — Part 1…

Welcome back peeps. Hope all’s well at your end. In this post we will cover the basics of regular expressions in NLP.
Some of the other best Series —
100 days : Your Data Science and Machine Learning Degree Series with projects
Complete Data Visualization and Pre-processing Series with projects
Tech Newsletter —
If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :
Regular Expressions ( RegEx) is an important topic in NLP — these are nothing but the expressions/patterns used to find or match character combinations in text/strings. These are text-matching tool embedded in Python which are very useful in creating string searches/performing any modifications in Strings.

Some basics that you should know —
[ab] means either ”a” or ”b”
[a-c] means ”a” ”b” or ”c”
[^a-c] # anything but a, b, c
Wild cards which is ”.” means ”any character”
”ba*t” matches ”bt”, ”bat”, ”baat”, ”baaat” etc.
”ba+t” matches ”bat”, ”baat” etc. but not ”bt”
/[A-Z]/ → matches an uppercase letter
/[a-z]/ → matches a lowercase letter
/[0–9]/ → matches a single digit
C{1,4} means at least one and no more than four C’s
C{5,5} means exactly five C’s
cdf{2, }: matches ‘cd’ followed by 2 or more ‘c’
\char is Escaped special character
/[1234567890]/ → any digit
Functions —
start()–return the starting position of the match
end()–return the ending position of the match
span()–return (start,end) as a tuple
group()–return the string that matched
group()–the whole string
Quantifiers —
*? is 0 or more times
+? is 1 or more times
?? is 0 or 1 time
{n}? is Exactly n times
{n,}? is At least n times
{n,m}? is From n to m times
Find and replace matched patterns —
Validate match : Regex.IsMatch
Retrieve single match : Regex.Match (first)
Regex.Matches : Retrieve all matches
Replace match: Regex.Replace
Divide text : Regex.Split
Functions commonly used —
- re.search() : To find regular expression pattern in the given string input
Syntax : re.search(patterns, string)
- re.match() : To match the input string if the regex pattern is found
Syntax : re.match(patterns, string)
- re.sub() : To substitute a substring with another string input
Syntax : re.sub(patterns, substitute_substring, input string)
- re.compile()
Syntax : re.compile(patterns, repl, string)
- re.findall() : To find all the occurances of the given pattern in the input string
Syntax : re.findall(patterns, string)
- re.split() : To split the string based on the pattern
Syntax : re.split(pattern, string, maxsplit=0, flags=0)
- re.purge() : To clear regex cache
Regular expressions (regex) are a powerful tool for matching patterns in text, and they are widely used in natural language processing (NLP) for tasks such as tokenization, text cleaning, and information extraction.
A regular expression is a sequence of characters that defines a search pattern. The basic building blocks of a regular expression are:
- Literals: These are characters that match themselves, such as “a”, “b”, “1”, or “.”
- Special characters: These are characters that have a special meaning in regular expressions, such as “*”, “+”, “?”, “^”, “$”, and “.”
- Character classes: These are sets of characters that can match any one character within the set, such as “[a-z]” (any lowercase letter) or “[0–9]” (any digit).
- Anchors: These are special characters that match the position of the text, rather than the text itself, such as “^” (start of line) and “$” (end of line)
- Groups and Alternation: These are special characters that allow to group and alternate different parts of the pattern.
Once a regular expression is defined, it can be used to search through a piece of text to find matches. The process of matching a regular expression to text is called pattern matching.
For example, in NLP, a regular expression can be used to find all occurrences of email addresses in a text, or to remove all non-alphabetic characters from a text. Regular expressions can also be used for tokenization, splitting a text into individual words or sentences, by matching patterns of whitespace or punctuation.
One of the best documentation for the reference —
In the next post, we will see how these basic regex concepts can be used in NLP through a project.
Day 7 : Coming soon!
For complete 60 days of Data Science and ML : Day 1 — Day 60 : Quick Recap of 60 days of Data Science and ML
Follow for more updates. Stay tuned and keep coding!
For other projects, tune to —
Build Machine Learning Pipelines( With Code)
Recurrent Neural Network with Keras
Clustering Geolocation Data in Python using DBSCAN and K-Means
Facial Expression Recognition using Keras
Hyperparameter Tuning with Keras Tuner
Custom Layers in Keras





