Using Regular Expressions

According to Wikipedia, Regular Expressions are: “In theoretical computer science and formal language theory, 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. The concept arose in the 1950s, when the American mathematician Stephen Kleene formalized the description of a regular language, and came into common use with the Unix text processing utilities ed, an editor, and grep, a filter.”

As a programmer or developer you might come across regular expressions and you might need to understand how they work. A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can probably think of regular expressions as super wildcards. You might be familiar with wildcard notations such as *.pdf to find all Adobe PDF files in a file manager. The regex equivalent is «.*\.txt» .

What can you do with regular expressions? In this article we see some basic examples.

1. Matching a Username

Matching a username

We begin by telling the parser to find the beginning of the string (^), followed by any lowercase letter (a-z), number (0-9), an underscore, or a hyphen. Next, {3,16} makes sure that are at least 3 of those characters, but no more than 16. Finally, we want the end of the string ($).

my-us3r_n4m3

th1s1s-wayt00_l0ngt0beausername (too long)

We can also get some help from Microsoft.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s