Exactly What Is Regex Used For? A Complete Guide

Consistent Pricing for All: 5 Benefits of Using CPQ Software criminal record checks Regex Programming
https://www.pexels.com/photo/grayscale-photo-of-computer-laptop-near-white-notebook-and-ceramic-mug-on-table-169573/

WorldExecutivesDigest.com | Exactly What Is Regex Used For? A Complete Guide | First of all, exactly what is Regex used for, anyway? If you’re asking yourself the same question, you’re in luck. Click here for more today!

Most people have likely never heard of regular expressions, generally called regex, but they’ve been around for a long time. They date back to 1956 when a mathematician named Stephen Cole Kleene created a mathematical notation known as regular sets.

The computer-based version of regex came along a few years later when Ken Thompson, one of the original developers of the Unix operating system, used regular expressions in the ed text editor in 1968.

Nearly two decades later, in 1986, the POSIX (portable operating system interface) solidified the regex standard. Since then, these expressions have found their way into many different parts of computing.

What Does Regex Mean?

Regex is short for regular expression. It’s a way of describing patterns of text using symbols to make it easier to compare or search for specific patterns of text.

Plain text is the most universal file format, working across Windows, Mac, Linux, and virtually every other operating system that exists today or has ever existed. You could take a text file created 40 years ago and open it on any modern computer without any special software.

Regular expressions make it easy to manipulate that text.

Why You Would Use Regex

Computers are smart but they’re extremely literal. If you want to find a certain pattern of text in a longer document, it’s easy to search for that particular pattern. But if you want to find different variations of a pattern or limit the results based on other criteria, regex makes it a lot easier.

For example, you might want to find all instances of the word “test” but only if they’re at the end of a line. Or you might want to find all instances of the word “test” except the ones followed by the word “pattern”.

If you know how to use regex, these types of searches are easy.

What Is a Regex and How Does It Work?

A regex is a combination of symbols that tell the computer what to match, what not to match, and what limits you want to put on the results of the match. To the untrained eye, it looks like a bunch of random symbols but once you understand what they mean, it’s easy to follow.

A regex looks something like this:

/^[a-z0-9_-]{6,18}$/

This is a simple password matching regex, which is one of the most common applications for these expressions.

The ‘/’ characters mark the beginning and end of the regex. The ‘^’ and ‘$’ characters mark the beginning and end of the line. Everything in between spells out what pattern is allowed for the password.

In this example, the password can include any lower case letter from a to z, any number from 0 to 9, and either the underscore or dash symbol. Nothing else will match the pattern. And the {6,18} portion of the expression means it will match anything that has at least 6 characters and no more than 18.

In this example, the password ‘y0ur_p4ssword’ would match but ‘y0ur_p@ssword’ would not because the ‘@’ symbol is not part of the regex.

If you want to learn more about how to write a regex, check out https://setapp.com/how-to/regex-quick-start.

Common Uses for Regex

Matching passwords is one of the common uses for regex but there are quite a few others, including:

  • Testing for valid email addresses
  • Testing for valid postal codes
  • Text replacement in large documents
  • Counting different variations of a word
  • Converting non-printing characters such as new lines

Programming

Regex got its start in the ed text editor but the most common way it’s used in today’s world is in programming languages like Perl and Python. Any coder worth their salt knows how to use regex in Python or whatever their programming language of choice might be.

Regex is used to match all kinds of input from users on websites, in applications, on login pages, and in many other places. If you’ve ever seen a message that your phone number or email address was invalid when signing up for a website, you’ve seen regex in action.

A good coder can use these expressions to avoid typos like “yourname@domaincom” instead of “yourname@domain.com”.

Search and Replace in Text Files

Searching and replacing text is the reason regex exists in the first place. The original use back in 1968 was in the ed text editor, to make it easy to find specific patterns of text.

Today’s text editing apps are virtually unrecognizable when compared to that original version of ed but if someone from 1968 traveled ahead in time by 50 years, they could still write a regex that would work in modern software.

How to Learn Regex

You don’t need to be a programmer to reap the benefits of regex. A lot of common applications support regex searches, even if it’s not always obvious in the app. Because most people aren’t familiar with it, apps tend to hide the feature so it doesn’t confuse people.

Any text editor worth its salt, such as BBEdit on the Mac or Notepad++ on Windows supports regular expressions. But even many common office applications, such as Microsoft Excel, include this powerful search feature as well.

If you want to learn how to use these expressions to save time and perform more powerful searches, there are several ways to learn. The easiest way to learn how to use regex is to Google something like “regex tutorial”. You’ll find a ton of websites that teach you how it works from the simplest uses right through extremely advanced power-user applications.

You can also get apps like Regex Lab for the iPhone and iPad that will teach you how to use it and let you practice in the app itself.

You Don’t Have to Be a Techie to Use Regex

Don’t discount regex as something for geeks and coders. If you take the time to learn it, you’ll find all kinds of ways it can make your life easier.

Was this post helpful? Be sure to check out the rest of our blog for more interesting ideas about getting more out of your technology.

Image