Regex Tutorial - How to write Regular Expressions?
Last Updated :
12 Apr, 2024
A regular expression (regex) is a sequence of characters that define a search pattern. Here's how to write regular expressions:
- Start by understanding the special characters used in regex, such as ".", "*", "+", "?", and more.
- Choose a programming language or tool that supports regex, such as Python, Perl, or grep.
- Write your pattern using the special characters and literal characters.
- Use the appropriate function or method to search for the pattern in a string.
Examples:
- To match a sequence of literal characters, simply write those characters in the pattern.
- To match a single character from a set of possibilities, use square brackets, e.g. [0123456789] matches any digit.
- To match zero or more occurrences of the preceding expression, use the star (*) symbol.
- To match one or more occurrences of the preceding expression, use the plus (+) symbol.
- It is important to note that regex can be complex and difficult to read, so it is recommended to use tools like regex testers to debug and optimize your patterns.
A regular expression (sometimes called a rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace" like operations. Regular expressions are a generalized way to match patterns with sequences of characters. It is used in every programming language like C++, Java and Python.
What is a regular expression and what makes it so important?
Regex is used in Google Analytics in URL matching in supporting search and replaces in most popular editors like Sublime, Notepad++, Brackets, Google Docs, and Microsoft Word.
Example : Regular expression for an email address :
^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$
The above regular expression can be used for checking if a given set of characters is an email address or not.
How to write regular expressions?
There are certain elements used to write regular expressions as mentioned below:
1. Repeaters ( *, +, and { } )
These symbols act as repeaters and tell the computer that the preceding character is to be used for more than just one time.
2. The asterisk symbol ( * )
It tells the computer to match the preceding character (or set of characters) for 0 or more times (upto infinite).
Example : The regular expression ab*c will give ac, abc, abbc, abbbc….and so on
3. The Plus symbol ( + )
It tells the computer to repeat the preceding character (or set of characters) at atleast one or more times(up to infinite).
Example : The regular expression ab+c will give abc, abbc,
abbbc, … and so on.
4. The curly braces { … }
It tells the computer to repeat the preceding character (or set of characters) for as many times as the value inside this bracket.
Example : {2} means that the preceding character is to be repeated 2
times, {min,} means the preceding character is matches min or more
times. {min,max} means that the preceding character is repeated at
least min & at most max times.
5. Wildcard ( . )
The dot symbol can take the place of any other symbol, that is why it is called the wildcard character.
Example :
The Regular expression .* will tell the computer that any character
can be used any number of times.
6. Optional character ( ? )
This symbol tells the computer that the preceding character may or may not be present in the string to be matched.
Example :
We may write the format for document file as – “docx?”
The ‘?’ tells the computer that x may or may not be
present in the name of file format.
7. The caret ( ^ ) symbol ( Setting position for the match )
The caret symbol tells the computer that the match must start at the beginning of the string or line.
Example : ^\d{3} will match with patterns like "901" in "901-333-".
8. The dollar ( $ ) symbol
It tells the computer that the match must occur at the end of the string or before \n at the end of the line or string.
Example : -\d{3}$ will match with patterns like "-333" in "-901-333".
9. Character Classes
A character class matches any one of a set of characters. It is used to match the most basic element of a language like a letter, a digit, a space, a symbol, etc.
\s: matches any whitespace characters such as space and tab.
\S: matches any non-whitespace characters.
\d: matches any digit character.
\D: matches any non-digit characters.
\w : matches any word character (basically alpha-numeric)
\W: matches any non-word character.
\b: matches any word boundary (this would include spaces, dashes, commas, semi-colons, etc.
[set_of_characters]: Matches any single character in set_of_characters. By default, the match is case-sensitive.
Example : [abc] will match characters a,b and c in any string.
10. [^set_of_characters] Negation:
Matches any single character that is not in set_of_characters. By default, the match is case-sensitive.
Example : [^abc] will match any character except a,b,c .
11. [first-last] Character range:
Matches any single character in the range from first to last.
Example : [a-zA-z] will match any character from a to z or A to Z.
12. The Escape Symbol ( \ )
If you want to match for the actual ‘+’, ‘.’ etc characters, add a backslash( \ ) before that character. This will tell the computer to treat the following character as a search character and consider it for a matching pattern.
Example : \d+[\+-x\*]\d+ will match patterns like "2+2"
and "3*9" in "(2+2) * 3*9".
13. Grouping Characters ( )
A set of different symbols of a regular expression can be grouped together to act as a single unit and behave as a block, for this, you need to wrap the regular expression in the parenthesis( ).
Example : ([A-Z]\w+) contains two different elements of the regular
expression combined together. This expression will match any pattern
containing uppercase letter followed by any character.
14. Vertical Bar ( | )
Matches any one element separated by the vertical bar (|) character.
Example : th(e|is|at) will match words - the, this and that.
15. \number
Backreference: allows a previously matched sub-expression(expression captured or enclosed within circular brackets ) to be identified subsequently in the same regular expression. \n means that group enclosed within the n-th bracket will be repeated at current position.
Example : ([a-z])\1 will match “ee” in Geek because the character
at second position is same as character at position 1 of the match.
16. Comment ( ?# comment )
Inline comment: The comment ends at the first closing parenthesis.
Example : \bA(?#This is an inline comment)\w+\b
17. # [to end of line]
X-mode comment. The comment starts at an unescaped # and continues to the end of the line.
Example : (?x)\bA\w+\b#Matches words starting with A
Similar Reads
Natural Language Processing (NLP) Tutorial Natural Language Processing (NLP) is a branch of Artificial Intelligence (AI) that helps machines to understand and process human languages either in text or audio form. It is used across a variety of applications from speech recognition to language translation and text summarization.Natural Languag
5 min read
Introduction to NLP
Natural Language Processing (NLP) - OverviewNatural Language Processing (NLP) is a field that combines computer science, artificial intelligence and language studies. It helps computers understand, process and create human language in a way that makes sense and is useful. With the growing amount of text data from social media, websites and ot
9 min read
NLP vs NLU vs NLGNatural Language Processing(NLP) is a subset of Artificial intelligence which involves communication between a human and a machine using a natural language than a coded or byte language. It provides the ability to give instructions to machines in a more easy and efficient manner. Natural Language Un
3 min read
Applications of NLPAmong the thousands and thousands of species in this world, solely homo sapiens are successful in spoken language. From cave drawings to internet communication, we have come a lengthy way! As we are progressing in the direction of Artificial Intelligence, it only appears logical to impart the bots t
6 min read
Why is NLP important?Natural language processing (NLP) is vital in efficiently and comprehensively analyzing text and speech data. It can navigate the variations in dialects, slang, and grammatical inconsistencies typical of everyday conversations. Table of Content Understanding Natural Language ProcessingReasons Why NL
6 min read
Phases of Natural Language Processing (NLP)Natural Language Processing (NLP) helps computers to understand, analyze and interact with human language. It involves a series of phases that work together to process language and each phase helps in understanding structure and meaning of human language. In this article, we will understand these ph
7 min read
The Future of Natural Language Processing: Trends and InnovationsThere are no reasons why today's world is thrilled to see innovations like ChatGPT and GPT/ NLP(Natural Language Processing) deployments, which is known as the defining moment of the history of technology where we can finally create a machine that can mimic human reaction. If someone would have told
7 min read
Libraries for NLP
NLTK - NLPNatural Language Toolkit (NLTK) is one of the largest Python libraries for performing various Natural Language Processing tasks. From rudimentary tasks such as text pre-processing to tasks like vectorized representation of text - NLTK's API has covered everything. In this article, we will accustom o
5 min read
Tokenization Using SpacyBefore we get into tokenization, let's first take a look at what spaCy is. spaCy is a popular library used in Natural Language Processing (NLP). It's an object-oriented library that helps with processing and analyzing text. We can use spaCy to clean and prepare text, break it into sentences and word
3 min read
Python | Tokenize text using TextBlobTokenization is a fundamental task in Natural Language Processing that breaks down a text into smaller units such as words or sentences which is used in tasks like text classification, sentiment analysis and named entity recognition. TextBlob is a python library for processing textual data and simpl
3 min read
Hugging Face Transformers IntroductionHugging Face is an online community where people can team up, explore, and work together on machine-learning projects. Hugging Face Hub is a cool place with over 350,000 models, 75,000 datasets, and 150,000 demo apps, all free and open to everyone. In this article we are going to understand a brief
10 min read
NLP Gensim Tutorial - Complete Guide For BeginnersThis tutorial is going to provide you with a walk-through of the Gensim library.Gensim : It is an open source library in python written by Radim Rehurek which is used in unsupervised topic modelling and natural language processing. It is designed to extract semantic topics from documents. It can han
14 min read
NLP Libraries in PythonIn today's AI-driven world, text analysis is fundamental for extracting valuable insights from massive volumes of textual data. Whether analyzing customer feedback, understanding social media sentiments, or extracting knowledge from articles, text analysis Python libraries are indispensable for data
15+ min read
Text Normalization in NLP
Normalizing Textual Data with PythonIn this article, we will learn How to Normalizing Textual Data with Python. Let's discuss some concepts : Textual data ask systematically collected material consisting of written, printed, or electronically published words, typically either purposefully written or transcribed from speech.Text normal
7 min read
Regex Tutorial - How to write Regular Expressions?A regular expression (regex) is a sequence of characters that define a search pattern. Here's how to write regular expressions: Start by understanding the special characters used in regex, such as ".", "*", "+", "?", and more.Choose a programming language or tool that supports regex, such as Python,
6 min read
Tokenization in NLPTokenization is a fundamental step in Natural Language Processing (NLP). It involves dividing a Textual input into smaller units known as tokens. These tokens can be in the form of words, characters, sub-words, or sentences. It helps in improving interpretability of text by different models. Let's u
8 min read
Python | Lemmatization with NLTKLemmatization is a fundamental text pre-processing technique widely applied in natural language processing (NLP) and machine learning. Serving a purpose akin to stemming, lemmatization seeks to distill words to their foundational forms. In this linguistic refinement, the resultant base word is refer
6 min read
Introduction to StemmingStemming is a method in text processing that eliminates prefixes and suffixes from words, transforming them into their fundamental or root form, The main objective of stemming is to streamline and standardize words, enhancing the effectiveness of the natural language processing tasks. The article ex
8 min read
Removing stop words with NLTK in PythonIn natural language processing (NLP), stopwords are frequently filtered out to enhance text analysis and computational efficiency. Eliminating stopwords can improve the accuracy and relevance of NLP tasks by drawing attention to the more important words, or content words. The article aims to explore
9 min read
POS(Parts-Of-Speech) Tagging in NLPOne of the core tasks in Natural Language Processing (NLP) is Parts of Speech (PoS) tagging, which is giving each word in a text a grammatical category, such as nouns, verbs, adjectives, and adverbs. Through improved comprehension of phrase structure and semantics, this technique makes it possible f
11 min read
Text Representation and Embedding Techniques
One-Hot Encoding in NLPNatural Language Processing (NLP) is a quickly expanding discipline that works with computer-human language exchanges. One of the most basic jobs in NLP is to represent text data numerically so that machine learning algorithms can comprehend it. One common method for accomplishing this is one-hot en
9 min read
Bag of words (BoW) model in NLPIn this article, we are going to discuss a Natural Language Processing technique of text modeling known as Bag of Words model. Whenever we apply any algorithm in NLP, it works on numbers. We cannot directly feed our text into that algorithm. Hence, Bag of Words model is used to preprocess the text b
4 min read
Understanding TF-IDF (Term Frequency-Inverse Document Frequency)TF-IDF (Term Frequency-Inverse Document Frequency) is a statistical measure used in natural language processing and information retrieval to evaluate the importance of a word in a document relative to a collection of documents (corpus). Unlike simple word frequency, TF-IDF balances common and rare w
6 min read
N-Gram Language Modelling with NLTKLanguage modeling is the way of determining the probability of any sequence of words. Language modeling is used in various applications such as Speech Recognition, Spam filtering, etc. Language modeling is the key aim behind implementing many state-of-the-art Natural Language Processing models.Metho
5 min read
Word Embedding using Word2VecWord Embedding is a language modelling technique that maps words to vectors (numbers). It represents words or phrases in vector space with several dimensions. Various methods such as neural networks, co-occurrence matrices and probabilistic models can generate word embeddings.. Word2Vec is also a me
6 min read
Pre-trained Word embedding using Glove in NLP modelsIn modern Natural Language Processing (NLP), understanding and processing human language in a machine-readable format is essential. Since machines interpret numbers, it's important to convert textual data into numerical form. One of the most effective and widely used approaches to achieve this is th
7 min read
Overview of Word Embedding using Embeddings from Language Models (ELMo)What is word embeddings? It is the representation of words into vectors. These vectors capture important information about the words such that the words sharing the same neighborhood in the vector space represent similar meaning. There are various methods for creating word embeddings, for example, W
2 min read
NLP Deep Learning Techniques
NLP Projects and Practice
Sentiment Analysis with an Recurrent Neural Networks (RNN)Recurrent Neural Networks (RNNs) are used in sequence tasks such as sentiment analysis due to their ability to capture context from sequential data. In this article we will be apply RNNs to analyze the sentiment of customer reviews from Swiggy food delivery platform. The goal is to classify reviews
5 min read
Text Generation using Recurrent Long Short Term Memory NetworkLSTMs are a type of neural network that are well-suited for tasks involving sequential data such as text generation. They are particularly useful because they can remember long-term dependencies in the data which is crucial when dealing with text that often has context that spans over multiple words
4 min read
Machine Translation with Transformer in PythonMachine translation means converting text from one language into another. Tools like Google Translate use this technology. Many translation systems use transformer models which are good at understanding the meaning of sentences. In this article, we will see how to fine-tune a Transformer model from
6 min read
Building a Rule-Based Chatbot with Natural Language ProcessingA rule-based chatbot follows a set of predefined rules or patterns to match user input and generate an appropriate response. The chatbot canât understand or process input beyond these rules and relies on exact matches making it ideal for handling repetitive tasks or specific queries.Pattern Matching
4 min read
Text Classification using scikit-learn in NLPThe purpose of text classification, a key task in natural language processing (NLP), is to categorise text content into preset groups. Topic categorization, sentiment analysis, and spam detection can all benefit from this. In this article, we will use scikit-learn, a Python machine learning toolkit,
5 min read
Text Summarizations using HuggingFace ModelText summarization is a crucial task in natural language processing (NLP) that involves generating concise and coherent summaries from longer text documents. This task has numerous applications, such as creating summaries for news articles, research papers, and long-form content, making it easier fo
5 min read
Advanced Natural Language Processing Interview QuestionNatural Language Processing (NLP) is a rapidly evolving field at the intersection of computer science and linguistics. As companies increasingly leverage NLP technologies, the demand for skilled professionals in this area has surged. Whether preparing for a job interview or looking to brush up on yo
9 min read