Short for regular expression, a regex is a string of text that lets you create patterns that help match, locate, and manage text. Perl is a great example of a programming language that utilizes regular expressions. However, its only one of the many places you can find regular expressions. Regular expressions can also be used from the command line and in text editors to find text within a file.
When first trying to understand regular expressions, it seems as if it’s a different language. However, mastering regular expressions can save you thousands of hours if you work with text or need to parse large amounts of data. Below is an example of a regular expression with each of its components labeled. This regular expression is also shown in the Perl programming examples shown later on this page.
The basics of regular expressions (cheat sheet)
Looking at the above example may be overwhelming. However, once you understand the basic syntax of how regular expression commands operate you can read the above example as if you are reading this sentence. Unfortunately, not all programs, commands, and programming languages use the same regular expressions, but they all share similarities.
Escape characters (escape sequence)
Regular expression flags
Outside the regular expression (at the end) flags helps with the pattern matching.
Escape characters are case sensitive.
Perl programming language regular expression examples
Below are a few examples of regular expressions and pattern matching in Perl. Many of these examples are similar or the same to other programming languages and programs that support regular expressions.
$data =~ s/bad data/good data/i;
The above example replaces any “bad data” with “good data” using a case-insensitive match. So if the $data variable was “Here is bad data” it would become “Here is good data”.
$data =~ s/a/A/;
This example replaces any lowercase a with an uppercase A. So if $data was “example” it would become “exAmple”.
$data =~ s/[a-z]/*/;
The above example replaces any lowercase letter, a through z, with an asterisk. So if $data was “Example” it would become “E******”.
$data =~ s/e$/es/;
This example uses the $ character, which tells the regular expression to match the text before it at the end of the string. So if $data was “example” it would become “examples”.
$data =~ s/./!/;
In the above example, we are replacing a period with an exclamation mark. Because the period is a metacharacter if you only entered a period without the \ ( escape) it is treated as any character. In this example, if $data were “example.” it would become “example!”, however, if you did not have the escape it would replace every character and become “!!!!!!!!”
$data =~ s/^e/E/;
Finally, in this above example the caret ( ^ ) tells the regular expression to match anything at the beginning of the line. In this example, any lowercase “e” at the beginning of the line is replaced with a capital “E.” Therefore, if $data was “example” it would become “Example”.
Computer acronyms, Escape sequence, Expression, Glob, Meta-character, Programming terms, Tilde, Wildcard
If you want to explore regular expressions even more in commands like grep, or regular expressions in programming language’s check out the O’Reilly book “Mastering regular expressions.”
Related information
- Use our text tool to perform common regular expressions online.
- Regular Expressions Quick Reference
- How to find and replace text within a text file.
- How to create a computer program.