Skip to main content

Just a few regular expressions (regex) I regularly use.

# Leading and trailing whitespace
^[ \s]+|[ \s]+$

# Multiple spaces replacement
\s+

# Removes any lines that don't start with `ERROR`
^(?!(?:ERROR)).*\s*

# Remove all standard C-style comments and inline comments (greedy)
(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)

# Find non-ASCII characters
[^\x00-\x7F]

# HTML Tags except `<p> </p>`
<(?>/?)(?!p).+?>

# Non-alphanumeric replacement
[^a-zA-Z0-9]

# Blank line
^$

# Positive integers
^[1-9]+[0-9]*$

# Positive decimal values
(^\d*\.?\d*[0-9]+\d*$)|(^[0-9]+\d*\.\d*$)

# Percentage (2 decimal places)
^-?[0-9]{0,2}(\.[0-9]{1,2})?$|^-?(100)(\.[0]{1,2})?$

# State abbreviation
[A-Z][A-Z]

# Phone Numbers
(^\+[0-9]{2}|^\+[0-9]{2}\(0\)|^\(\+[0-9]{2}\)\(0\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\-\s]{10}$)

# Email address
^[\\w\\-]+(\\.[\\w\\-]+)*@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$
^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$
^([\w\.*\-*]+@([\w]\.*\-*)+[a-zA-Z]{2,9}(\s*;\s*[\w\.*\-*]+@([\w]\.*\-*)+[a-zA-Z]{2,9})*)$  ### List of semi-colon seperated email addresses
^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$

# IP Address
^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$

# City, State abbreviation
.*, [A-Z][A-Z]

# Zip Code (84094 or 84094-1234)
[0-9]\{5\}(-[0-9]\{4\})?

# Social security number, such as: ###-##-####
[0-9]\{3\}-[0-9]\{2\}-[0-9]\{4\}

# Dollar amounts, specified with a leading $ symbol
\$[0-9]*.[0-9][0-9]

# Date
[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}             ### 2003-08-06
[A-Z][a-z][a-z] [0-9][0-9]*, [0-9]\{4\}      ### Jan 3, 2003
^(\d{1,2})\/(\d{1,2})\/(\d{2}|(19|20)\d{2})$ ### DD/MM/YY or DD/MM/YYYY or MM/DD/YY or MM/DD/YYYY

# Font tags replacement
<(FONT|font)([ ]([a-zA-Z]+)=("|')[^"\']+("|'))*[^>]+>([^<]+)(</FONT>|</font>

# Url
^http(s)?:\/\/((\d+\.\d+\.\d+\.\d+)|(([\w-]+\.)+([a-z,A-Z][\w-]*)))(:[1-9][0-9]*)?(\/([\w-.\/:%+@&=]+[\w- .\/?:%+@&=]*)?)?(#(.*))?$/i

# Find and Replace Regex in Sublime Text
Description..: Insert a span tag inside an anchor tag (retains anchor contents)
Find.........: (<a.*>)(.*)</a>
Replace......: $1<span class="icon3 pdf">$2</span></a>