"http://www.w3.org/TR/xhtml1/DTD/transitional.dtd"> XSLT Programmer's Notebook

BizTalk Utilities CV ,   Jobs ,   Code library  
 
 

  Please Hang Up and Dial Again!

Declarative tools are the path for VB programmers to becoming more productive. Need an example? Listing 1 is a function that validates a phone number. It comes from "VB for the web" book from a respected publisher. This function does what it is supposed to, but regular expressions allow us to do the same thing better and with less code. As you can see the pure VB function is a lot of code. Perhaps you will say it is only about 20 lines of instructions-that's not that much coding. But, remember is not just coding but maintaining it, compiling it, "SourceSafeing" it and documenting it. So 20 lines of code (and some constants) is a lot. Also, think of performance. The pure VB function calls Mid$ 12 times and IsNumeric 10 times. And yet for all this code, the function only accepts one format namely ### ###-####

So what would the function using a regex look like?

The Regex way
		
Private Function IsPhoneValid(ByVal strPhone As String) As Boolean
   'Validate the Phone Number passed is in the following
	Dim regex As New RegExp
	regex.Pattern = "^\(?([1-9]\d{2})\)?\s?([1-9]\d{2})[-\s]?(\d{4})$"
	IsPhoneValid = regex.Test(strPhone)
End Function

Listing 2 Huh?Although the regex itself looks like neo-Babylonian cuneiform, it is actually more on the order of the simpler Old Persian cuneiform.

It is true that I don't raise any special errors as we see in Listing 1. But if your customers need to be told to not enter letters in the phone number slot....well....

The regex in the function will validate the following formats.

### ###-####

(###) ###-####

(###)###-####

(###) ### ####

(###)### ####

### #######

##########

And it (correctly) does not validate numbers like 028 283-283 or (414) 093-2929 since area codes and exchanges don't start with zero.

 So what does ^\(?([1-9]\d{2})\)?\s?([1-9]\d{2})[-\s]?(\d{4})$mean?

Here is a brief functional synopsis

^ allow no whitespace at front
\(? The ( is escaped by \ the ? means it is optional
(

start of first subpattern (this does not match anything)

[1-9] one char from 1 to 9
\d{2} 2 decimal chars
) end of first submatch
\)? Optional end parenthesis
\s? optional white space
([1-9]\d{2}) second subpattern one char from 1-9 and two others
[-\s]? a hyphen or space
(\d{4}) third subpattern-four decimals
$ allow no white space at end

The documentation for VB's LIKE command is about a page and a half. Regexes are a little more complex. Mastering Regular Expressions is 316 pages long and makes references to magazine articles for more details. Although seemingly daunting to the uninitiated, there are plenty of articles about using regexes.

What's that? You are not daunted? Well we didn't even get into minimal matching, backreferences and lookaheads!

Although the XML DOM will in the long run make Regexes less important because things will be already sliced and diced, it is still going an important tool among smart and good-looking programmers..well smart programmers.

Unfortunately, I am not going to be able to answer email about regexes. Not until I am down to two jobs. A good article on MSDN is found here.

 

Recent Jobs

Integration Specialist Needed - Wor
Virtualization Server Infrastructur
A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En

View all Jobs (Add yours)
View all CV (Add yours)



Information Online

swimming pool builder
chicago web site design
spfxmasks
Cheap Web Hosting
conference calling
Prada sunglasses
answering service


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP