Email Regular Expression

One of my colleagues asked a question that I’ve heard dozens of times over my career…

What’s a good regular expression for validating email addresses.

Sadly, due to poor standards, poor implementation choices, and just the sheer age of Email, this is a surprisingly tough problem.

The best way to validate an email address is to email it, and get the user to do something. That’s not really feasible if it’s someone just filling in a form.

Failing that, I came up with:

^[^@\s]+@[^@\s]+\.[^@\s]+$

This is the same as suggested by Microsoft, which is gratifying. You can see the logic of it here.

Advertisement
Email Regular Expression

The Importance of knowing Regular Expressions

It’s something that always mystifies me, it does seem that a lot of developers don’t know regular expression syntax very well. This came up when I was on some SharePoint 2013 training just before Christmas.

SharePoint 2013 introduces something called “Routing Rules”. These are rules that allow you to direct traffic to different front-end servers (or pools of servers), allowing you to isolate traffic, route to better health servers, etc.. Spence Harbar has a very good article about it.

Anyway, one some of the criteria that you can match rules on are Regular Expressions. However, SharePoint does warn you that Regex routing rules are slower – this is unsurprising. But how much slower than, say, a ‘Starts with’ or ‘Ends with’ rule? And on the Ignite training I did wonder about the efficiency of their example… Continue reading “The Importance of knowing Regular Expressions”

The Importance of knowing Regular Expressions

HTML 5 – Email Address Validation Regex

And interesting one to stumble upon – the ‘official‘ regex for validating a email address:

Edit: I’ve had to break this down into parts – for some reason WordPress throws a 403 error if I don’t. I think it sees this regex as a security risk!

Before the @ sign:
^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+

The @ sign and after:
@[a-zA-Z0-9-]+(?:.[a-zA-Z0-9-]+)*$

Reads like it should work pretty well to me. I didn’t realise before the @ was so generous. And you could shrink it by using a case insensitive operator.

HTML 5 – Email Address Validation Regex

Lookbehinds, Regexes, and replacing n

This is a little note for myself; don’t forget lookbehinds (and lookaheads) in regular expressions as a way of matching text that you don’t want to replace.

For example, if converting new lines to carriage-return new-lines:

// n ---> rn
string output = Regex.Replace(input, "(?<!r)n", "rn"); 

This pattern find any new line character ‘n‘ and checks if the preceding character ‘(?< … )‘ is not a carriage return ‘!r‘.

This is neater than my having a capture group for the preceding character, and then having to put that group into my replacement pattern.

Lookbehinds, Regexes, and replacing n

Get query string parameters with Regular Expressions (in JavaScript)!

I’ve said it before, but not quite so explicitly – it’s easiest to get query string parameters with a regular expression. I keep seeing big JavaScript functions looping through and splitting the parameters up, and that might be fine if you want to store those and use them repeatedly – but if not, regexes are your friend.

This function will return the value of a parameter. If the parameter requested isn’t in the query string, it returns null.If the parameter doesn’t have a value (e.g. debug in ...&debug&...) it returns an empty string.

function GetQueryParam(parameter){
var p = escape(unescape(parameter));
var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
var match = regex.exec(window.location.search);
var value = null;
if( match != null ){
value = match[1];
}
return value;
}

Get query string parameters with Regular Expressions (in JavaScript)!