Regex

A regular expression, abbreviated as RegEx or regex, is a pattern for locating a character string in a program. A regular expression can have either only regular (or simple) characters or a combination of regular and special characters. A regular expression is constructed by using normal slashes.

The purpose of using regular expressions is to identify patterns that a programmer can then act upon. Some common examples might include finding and replacing unwanted characters in a string, querying a string or group of strings to find specific information, or validating input.

Most programming languages that support Regex, regular expressions, and pattern searching have modules or libraries that support them.

To introduce a regular expression, use the import module in your program:

import re

The search method varies between programming languages. One example is using test( ) or exec( ), which stands for execute, and entering the character string between the parentheses.

Special characters in regular expressions indicate different patterns, repeats, or spaces. For example, if a program contains a set of numbers, and a regular expression is supposed to search every instance in a character string that has that set of numbers, special characters are used to indicate that pattern of numbers. The program will then know to locate each instance of that set of numbers.

To escape special characters, use backslashes around the expression. This avoids the usual function of a special character, so that they read as their literal value.

 

Learn more about regular expressions in Mozilla’s JavaScript Guide.

James Payne contributed to this article.

Jenna Phipps
Jenna Phipps is a contributor for websites such as Webopedia.com and Enterprise Storage Forum. She writes about information technology security, networking, and data storage. Jenna lives in Nashville, TN.

Top Articles

List of Windows Operating System Versions & History [In Order]

The Windows operating system (Windows OS) refers to a family of operating systems developed by Microsoft Corporation. We look at the history of Windows...

How to Create a Website Shortcut on Your Desktop

Website Shortcut on Your Desktop reviewed by Web Webster   This Webopedia guide will show you how to create a website shortcut on your desktop using...

What are the Five Generations of Computers? (1st to 5th)

Reviewed by Web Webster Learn about each of the 5 generations of computers and major technology developments that have led to the computing devices that...

Hotmail [Outlook] Email Accounts

Launched in 1996, Hotmail was one of the first public webmail services that could be accessed from any web browser. At its peak in...

Network Monitoring Software

Network monitoring software is an application that observes traffic and device...

New Promoter Score (NPS)

Source: Freepik for flaticon.com Net promoter...

Data Annotation

Data annotation involves processing a set of raw data for text, images, sounds,...