RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using Smart Tags in Office 2003 : Page 2

Smart tags in Office 2003 have been improved to make them easier to develop and to address a few limitations that were found in Office XP. You can now create recognizers for smart tag Lists, which are XML documents, by using regular expressions. The Type Library has also been extended for more power when creating smart tags via code.

MOSTL and Regular Expressions
Regular expressions are a powerful form of wildcard searches. Most developers (and even power users) are familiar with regular expressions used to search for files, such as *.doc. When it comes to regular expression pattern matching, you will need more sophisticated patterns. The following pattern matches US phone numbers within a text string.
If you are not familiar with regular expressions, you might find this but of code a little difficult to read. Let's take a brief look at the syntax of regular expressions. Table 1 lists the most common meta-characters that are used in regular expression patterns.

Table 1: This Smart Tag Meta-character listing will make it easy to follow the exploration.
Meta-Character Meaning
Character Matching  
. (period) Matches any single character except the newline character
[] Matches any one of the enclosed characters. Ranges can be specified by using a hyphen, such as [0-9]
| The OR operator. For example, x|y will match either x or y
Position Matching  
^ Matches the beginning of string
$ Matches the end of string
Repetition Matching  
? Matches 0 or 1 instances of the preceding character
+ Matches 1 or more instances of the preceding character
\ Indicates that the next character should not be interpreted as a regular expression special character
* Matches 0 or more instances of the preceding character
() Groups a series of characters together
{n} Matches exactly n instances of the preceding character
{n,} Matches at least n instances of the preceding character
{n,m} Matches at lease n but no more than m instances of the preceding character
Special Characters  
\s Matches any single white space character, including space, tab, line feed, and form feed
\w Matches any alphanumeric character, including the underscore character
\d Matches a single-digit character
\f Matches a form feed
\n Matches a line feed
\r Matches a carriage return
\t Matches a tab
\v Matches a vertical tab
\b Matches a word boundary, such as a space

Examine this by breaking down the phone number pattern shown above. Start at the end of the pattern:
This means that the expression must contain exactly three digits, followed by a hyphen, and then exactly four digits. Thus, 555-1234 is a match. Now look at the first part:
Notice that there is a pipe (|) in the middle, indicating an or operator. On the right side of that you can see:
This will match exactly three digits followed by a hyphen. On the left side of the or (|) you see:
Notice that \( and \) are used to indicate that parenthesis are part of the pattern. In between these is the \d{3} indicating exactly three digits. So the pattern matches phone numbers in these formats:
This portion of the pattern ends with a question mark, indicating that the pattern immediately prior to the question mark is optional. Because the area code pattern is grouped in parentheses and the question mark is outside the parentheses, the entire pattern inside the parentheses is optional. The pattern will also be a match:
You could get even more sophisticated and match 281.866.7444 in addition to allowing a space between the various parts of the phone number and match foreign phone numbers.

Here's another example. Let's say you use a bug-tracking Web site to track issues that need to be addressed in your current project. You can define a smart tag that recognizes "Bug #123456" or "bug 123456" and link directly to that bug's details on the Web site. Here is the regular expression that matches these phrases:
You can create a smart tag definition to recognize this pattern and link to the Web site. It looks like Listing 2. Note that the URL in this example is a placeholder that should be replaced with the URL to your bug-tracking Web site. The resulting smart tag menu is shown in Figure 4.

Thanks for your registration, follow us on our social networks to keep up-to-date