Some Handy Regular Expressions

At least something
/.+/

A positive integer
/^\d*$/

A decimal number
/^\d*\.?\d/

A valid email address
/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/

A valid web address
/^(http:\/\/)?\w+([\.-]?\w+)*\w+([\.-]?\w+)*(\.\w+)+(\/)?(\w+)?$/

A hexadecimal colour (6 chars)
/^#?[0-9A-Fa-f]{6}$/

How to use in Javascript:
var testString = "ff0000";
var pattern = /^#?[0-9A-Fa-f]{6}$/;
if (testString.match(pattern)) DoSomething();

How to use in PHP:
$testString = 'ff0000';
$pattern = '/^#?[0-9A-Fa-f]{6}$/';
if (preg_match($pattern, $testString) DoSomething();

How to use in C#:
string testString = "ff0000";
string pattern = @"^#?[0-9A-Fa-f]{6}$";
Match m = Regex.Match(testString, pattern);
if (m.Success) DoSomething();

Tags: , ,

4 Responses to “Some Handy Regular Expressions”

  1. Frank says:

    How familiar are you with php coding Damian?

    I want to speed up the loading time for my blog… it takes a bit too long and I’m guessing that changing some of the php requests to HTML where possible will speed things up a little. You might be able to spot other things that could speed the whole thing up as well.

    Is that something you would be interested in having a go at? I’d love to pay for such a service, but I honestly can’t afford it. :)

  2. Damian says:

    Hi Frank.
    I wouldn’t even think of charging!
    Unfortunately I’m crazy-busy on a pretty big project at the moment so I can’t afford to take time out to look at any code. I have got some recommendations though:

    I tried pinging your server and it’s reasonably high. I get an average of around 48ms to my server and yours is getting about 180ms. A slight delay between user and server can make quite a bit of difference (as I’ll talk about later).

    It looks like your host is offshore (Dreamhost in California?) and that may account for the slight lag. If your audience is predominantly NZers then I’d recommend moving back home. I use OpenHost for my servers. They’ve got plans starting at $6.99+GST a month and I believe they’re hosted out of EDS in Takapuna.

    Next, I’d look at reducing the number of javascript links on the site. I counted 14 different external links. Each one of these chews up a bit of time at both the server and the client end and most of them are for adverts, trackers or cosmetic features.

    When a person first hits your homepage they have to download 263k over 37 HTTP requests (mine is 15k over 5 requests). Given the slightly extra delay (as per the ping stats) for each trip back and forth to get each image, script and stylesheet it all adds up.

    There are some really good tools you can get for FireFox that can perform diagnostics on your website. I use FireBug, HTML Validator, YSlow, Web Developer and a couple of others on a daily basis. Grab FireBug and YSlow for starters. YSlow gives your site an F but provides a lot of handy tips to speed things up.

    Also (shameless plug) check out some of the handy tips at Nice.

    Good luck! Making a website speedy but presentable is a bit of an art form but immensely satisfying in the end.

  3. Frank says:

    Yes, it’s an art I enjoy and have only a very basic knowledge of. Ultimately I want everybody who stops at my site and I know that I get frustrated with the loading time, so if I am, others must be as well.

    I’ll be sticking with the current hosting as it is supplied for free by a friend who purchased a whole bunch of space with Dreamhost… but the other stuff I can work on. I may look at a leaner template and get rid of some of the silly things I’ve put on there.

  4. Frank says:

    *”stops at my site, to enjoy the experience”

Leave a Reply