What is a RegEx?

  • Post category:Tutorials
  • Reading time:3 mins read

What is a RegEx?

What is a RegEx

A regular expression (RegEx or RegExp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. The regex equivalent is «.*\.txt» .

But you can do much more with regular expressions. In a text editor like EditPad Pro or a specialized text processing tool like PowerGREP, you could use the regular expression «\b[A-Z0-9._%+-]+@[A-Z0-9.- ]+\.[A-Z]{2,4}\b» to search for an email address. Any email address, to be exact. A very similar regular expression (replace the first \b with ^ and the last one with $) can be used by a programmer to check if the user entered a properly formatted email address. In just one line of code, whether that code is written in Perl, PHP, Java, a .NET language or a multitude of other languages.

Applications & Languages That Support Regexes

There are many software applications and programming languages that support regular expressions. If you are a programmer, you can save yourself lots of time and effort. You can often accomplish with a single regular expression in one or a few lines of code what would otherwise take dozens or hundreds.

Not Only for Programmers

If you are not a programmer, you use regular expressions in many situations just as well. They will make finding information a lot easier. You can use them in powerful search and replace operations to quickly make changes across large numbers of files. A simple example is «gr[ae]y» which will find both spellings of the word grey in one operation, instead of two. There are many text editors and search and replace tools with decent regex support.

4.9/5 - (8 votes)
Avatar_Admin
Admin

Leave a Reply