US Phone Number Regular Expression

30 10 2008

Here’s a regex for validating/parsing USA-format phone numbers.

/^(?:(\d)[ \-\.]?)?(?:\(?(\d{3})\)?[ \-\.])?(\d{3})[ \-\.](\d{4})(?: ?x?(\d+))?$/

Try copy and pasting it in Rubular.

Capture Groups:
0: “1″ prefix, if included
1: Area code, if included
2: Prefix
3: Line Number
4: Extension, if included

It allows for a handful of formats as well. You may use dashes, dots, spaces or even nothing as separators, parentheses around area code are optional. Extension may be defined as simply another segment, or prefixed with “x”.

Examples that all work
155555555555555
1.555.555.5555 5555
(555) 555-5555 x5555
555-5555

Simple validation can be performed just by checking if it matches, more robust validation can be performed by validating each capture group.





Stack Overflow

17 10 2008

I just signed up on Stack Overflow, a programming QA site with a strong basis on community. It’s the most addicting website I’ve used since Facebook. Users ask questions and they appear on the main page in a feed showing the question and how many votes the question has in a fashion similar to digg.com. People then have the opportunity to respond, and each response may be voted on as helpful or not.

Users earn reputation points by asking questions, providing answers, and receiving votes. Users may also earn “Badges” by doing other tasks like filling out their profile, or providing a very good answer to a long-outdated question.

If you’re a developer, you owe it to yourself to check out Stack Overflow.





Intel’s Tiny Forbidden City

14 10 2008

Coincidence?





Closure vs. First-Class Function

8 10 2008

I said I wasn’t going to post anymore on this page, but this has been bugging me. Since this latest dynamic-language trend has been going on, people have been throwing around the word ‘closure’ like a Swedish meatball in a food fight.

A first-class function is a function that acts as an object or variable. They are sometimes called lambdas, anonymous functions, or procs. A closure is a special type of anonymous function that has access to external variables not passed in as arguments. In most cases with the popular dynamic languages (Javascript, Ruby, Groovy, etc…) first-class functions are indeed closures, but I believe that it is less important that they are closures as they are first-class functions.

To help clarify, in Javascript:

var lambda = function(x) {  return x + 2; };


This is an example of a first-class function. Technically it is also a closure, but the fact that it is is unimportant because it doesn’t reference and variable outside its scope.

var y = 35;
var closure = function(x) { return x+y; };


This time it IS important to call this function a closure (though it is also an anonymous function, lambda, etc…) because it references variable ‘y’ which is defined outside the scope of the function.

In Ruby a ‘block’ is a special type of anonymous function that is passed to another function as an argument. Because they have access to their parent scope’s variables, they are closures, but they are more importantly blocks. So call them that. Using ‘closure’ all the time just adds confusion as to what a closure really is.

Ruby block example:

['one', 'two', 'three'].each {|it| puts it }

The “{|it| puts it}” part is the block. It is not important that it is a closure, again, because it doesn’t reference external variables.

This is really a bunch of hot air, but I believe that it’s very important, especially when dealing with very technical subjects, to use the most appropriate nomenclature. Unless you’re referencing the fact that a lambda (etc..) is referencing external variables, don’t call it a closure.





You are now being redirected…

7 10 2008

I’m abandoning this blog. Yep, I just started it, but I can’t be happy unless I’m hosting things myself. So I’m preparing to launch http://www.alexeibroner.com/ and http://www.lex-machina.com/ to host my new blogs. I’ll be in the woodshed making things work for a couple more weeks and then I’ll be back up at the new addresses.

What’s to come? An article on RESTful architecture for PHP and a preview of my REST server framework (oh god, that’s terrible news… not another web framework!). The good news? It’s easier to use than Rails (really!), and will be available for PHP with ports to Ruby and Java/Groovy in the works (and possibly Python as well).





33 year old high school cheerleader

15 09 2008

Only in Wisconsin:
via Yahoo.

Isn’t that cute? She just wanted to be a cheerleader. While public humiliation is one thing, I think pressing charges is a bit extreme. I mean; Who doesn’t want to be a high school cheerleader?

Thanks for the link Charles.





Computing a hexidecimal hash for a string in Java

12 09 2008

Computing a hexidecimal hash for a string is a very common function used in web development. It’s so common that most scripting languages provide a MD5 function that does just that. e.g. MD5(“Your favorite string goes here”);

Well I’ve found myself needing to generate hashes in Java apps a couple of times, and have found myself very disappointed in the amount of code required to perform what seems to me like a very mundane task. Simply put, it’s a pain in the butt.

So, I wrote a simple Java class with a single static method to do just that. To use it just type:


String myHash = DigestUtil.computeHexDigest(DigestUtil.ALGORITHM_MD5, "your favorite string goes here");

And that’s that. Well, it’ll never be as simple as but this beats the pants off doing it manually.

So, without further adieu, here’s the code ripe for copying-and-pasting into your app. Sorry for the formatting, WordPress is giving me trouble.

–BEGIN CODE SNIPPET–

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestUtil
{
/**
* Constant representing the MD2 hash algorithm
*/
public static String ALGORITHM_MD2 = “MD2″;

/**
* Constant representing the MD5 hash algorithm
*/
public static String ALGORITHM_MD5 = “MD5″;

/**
* Constant representing the SHA-1 hash algorithm
*/
public static String ALGORITHM_SHA1 = “SHA-1″;

/**
* Constant representing the SHA-256 hash algorithm
*/
public static String ALGORITHM_SHA256 = “SHA-256″;

/**
* Constant representing the SHA-384 hash algorithm
*/
public static String ALGORITHM_SHA384 = “SHA-384″;

/**
* Constant representing the SHA-512 hash algorithm
*/
public static String ALGORITHM_SHA512 = “SHA-512″;

/**
*
* Computes the hexadecimal hash key for <em>input</em> using given <em>algorithm</em>
*
* @param algorithm
* @param input
* @return
* @throws NoSuchAlgorithmException
*
*/
public static String computeHexDigest(String algorithm, String input)
throws NoSuchAlgorithmException
{
byte [] inputBytes = input.getBytes();
MessageDigest md = MessageDigest.getInstance(algorithm);
byte [] digest = md.digest(inputBytes);
BigInteger bigNumber = new BigInteger(1, digest);
String hash = bigNumber.toString(16);

if ( hash.length() < 64 )
{
StringBuffer buf = new StringBuffer();

for ( int i = 1; i < 64 – hash.length(); i++ )
{
buf.append(“0″);
}

hash = buf.toString() + hash;
}

return hash;
}

}

–END CODE SNIPPET–





How to read this specification

5 09 2008

From section 1.6.1 of the W3C Editor’s Draft of the HTML5 specification:

This specification should be read like all other specifications. First, it should be read cover-to-cover, multiple times. Then, it should be read backwards at least once. Then it should be read by picking random sections from the contents list and following all the cross-references.





Rubular: web-based Ruby regular expression editor

4 09 2008

I just stumbled upon Rubular a regex editor for Ruby. I’ve already used it once for work and I’m quite sure I’ll be returning regularly (pun intended). Note: Ruby 1.8 uses a PCRE engine, so this is useful to users of regexen in other languages as well, including PHP, Java, and JavaScript. Most other languages have PCRE libraries available.

Just as an exercise I wrote a regex for parsing a url. I’m sure it can be improved on (for example, it won’t deal with an “@” symbol) but for regular web addresses it will be quite useful. Copy and paste into Rubular for a demo.

Regex:
^(\w+):(?:\/\/)?([\w.]+)(\/(?:\w*/)*)(\w+)(\.\w+)?(?:\?([\w=&]*))?$

Test string:
https://www.example.com/parent/child/resource.xml?query=something&sort=asc

I’m looking forward to when Ruby changes its regex engine to Onigurama, then I can do this:


^(?<protocol>\w+):(?:\/\/)?(?<domain>[\w.]+)(?<path>\/(?:\w*/)*)(?<resource>\w+)(?<format>\.\w+)?(?:\?(?:<query_string>[\w=&]*))?$

This way I can refer to each capture by name. Onigurama is currently supported by the TextMate text editor.





Vim assistant

29 08 2008

I’m sure this is old hat to many, but I must repost. Freaking hilarious. Found on vim.org.