October 9, 2013 362 Views 3 Comments

Regular Expressions (RegEx) – Quick Reference

SHARE THIS ARTICLE

Regular expression also known as regex is a sequence of characters/symbols that forms a search pattern. It is supported by most programming language including Perl, Javascript, PHP, C-Sharp and Java.

Commonly Used Symbols and Syntax

META CHARACTERS
^
Start of subject (or line in multiline mode)
$
End of subject (or line in multiline mode)
[
Start character class definition
]
End character class definition
|
Alternates ( a|b matches a or b )
(
Start subpattern
)
End subpattern
Escape character
BASE CHARACTER CLASSES
w
Any “word” character (a-z 0-9 _)
W
Any non-“word” character
s
Whitespace (space, tab, CRLF)
S
Any non-whitespace character
d
Digits (0-9)
D
Any non-digit character
.
Any character except newline
QUANTIFIERS
*
Zero or more
+
One or more
?
Zero or one occurrence
{n}
n occurrences exactly
{n,}
At least n occurrences
{,m}
At most m occurrences
{n,m}
Between n and m occurrences inclusive
[…]
list or range of characters (or both). [abc] means “any single character that is either a, b or c”
[^…]
any single character that is not in the class. [^0-9xyz] matches any single character that isn’t a digit and isn’t the letter x, y, or z
r
escape sequences r (carriage return)
t
escape sequences t (tab)

These are most of the commonly-used RegEx features, there are quite a few others you may want to explore. The complete manual is at www.pcre.org/pcre.txt

USEFUL REGULAR EXPRESSIONS

Here’s a list of some useful regular expressions that can make life as a developer less hassling.

Regular expression for username

Following pattern will match alphanumeric string including “_” of length 3 to 25.

^[a-z0-9_]{3,25}$

Regular expression for email

^[a-z0-9]+([_a-z0-9.-]+)*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$

Regular expression for Password

Password must contain letters a-zA-Z and at least one digit 0-9

(/^(?=.*d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/)

Explanation…

(/^(?=.*d) //should contain at least one digit
(?=.*[a-z]) //should contain at least one lower case
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{8,} //should contain at least 8 from the mentioned characters $/)

Regular expression for URL

^http[s]?://[a-z0-9-.]+.[a-z.]{2,5}(/[a-z0-9%-_.?=&@#]*)?$

Regular expression for Decimal value

^[0-9]+$

Regular expression for Float value

^[0-9]*.[0-9]*[1-9]+$

Regular expression for Removing Whitespaces

str.replace(/^s+|s+$|s+(?=s)/g, "")

Share your favorite RegEX using the comment form below…

  • Sukey

    Thanks for the password guidance here. We’ve got a client asking to improve the password security on the site through stronger passwords and I was stumbling a bit in terms of getting the syntax for a clean run. This is working perfectly!

  • John

    Getting better password security is very important! I have been working all morning on trying to figure out how to complete the code. Your tips helped me a great deal, thanks!

    • Berry

      Password security is very important, I agree, but when coding a site you should realize that you cannot control everything the user does. They should provide good, long, complex passwords to begin with.