Parsing and Pasting One-Line Street Addresses (AutoHotkey Multi-Paste Trick)

Another Pet Peeve…the Windows Copy-and-Paste Doesn’t Make It Easy to Insert Street Addresses and Postal Codes into Forms

I’ve noticed that many applications and Web pages list street addresses on just one line:

Jack Dunning, 1234 Main Street, Any Town, MI  90571

This makes sense and saves space when compared to a three or four-line address listing:

Jack Dunning
1234 Main Street
Any Town, MI  90571

However, when using the Windows Clipboard for a copy-and-paste operation, a person still needs to jump between the two windows a number of times—unless he or she uses a parse-and-paste tool such as MultiPaste.ahk.

MultiPasteStackedAddressSince my little MultiPaste AutoHotkey tool breaks out new lines, I can resolve the stacked form of the address into its components—mostly. The City, State Zipcode line continues to present a same-line problem (as shown at right).

As for the single-line form of the address shown at the top of this blog, it gets no help at all since it continues to appear on just one line in the MsgBox command window.

For example, if someone copies the address and phone number from a Web page (cropped image displayed below), the Clipboard captures all the information but a person would find it very difficult to enter the correct address pieces into discreet fields such as those found in many address databases:

MultiPasteAddressSelect

Very aggravating!

Parsing Street Addresses and Zipcodes

MultiPasteAddressMsgBox

To solve this problem, I added a little bit of code to my MultiPaste.ahk script (first discussed in “Brute Force Data-Set Copy-and-Paste (AutoHotkey Clipboard Technique)“) for parsing the entire selected address and pasting individual items. As a result, I can run the same tool for copying-and-pasting street addresses as I use for copying Web data tables and spreadsheet cells.

After I convert the commas found in street addresses into tab (`t) characters, the script does most of the work:

Clipboard := StrReplace(Clipboard, "`, " , "`t")

Since the script replaces commas, the StrReplace() function requires escaping the comma character with the backtick (`,). After executing the CTRL+ALT+F Hotkey combination, AutoHotkey copies the selected address to the Windows Clipboard, then replaces every comma with the tab character (`t). That one line of code makes the MultiPaste.ahk script work as a parser for any comma delimited data.

However, since accepted address formats do not place a comma before the five-digit Zipcode, I continue to have a problem with the postal codes. The Zipcode remains attached to the two-letter state in the parsed MsgBox.

Parsing Zipcodes

I needed to write special code to recognize the Zipcode, then insert the tab character in front of it. For this step, I turned to the RegExReplace() function:

Clipboard := RegExReplace(Clipboard, "\s(\d\d\d\d\d)", "`t$1")

The RegExReplace() function looks for a space (\s) followed by five consecutive numerical digits (\d). If found, AutoHotkey replaces the matched characters with the tab character plus the five digits. Works like a charm!

Cover 200Note: I use Regular Expressions (RegEx) a few different places in the MultPaste.ahk script. I understand that they can cause a good deal of confusion for anyone unfamiliar with them. Yet, in many cases, Regular Expressions provide a relatively simple method for accomplishing otherwise complicated tasks. Although initially not easy to grasp, AutoHotkey users will find it worthwhile to add RegExs to their bag of tricks. In my next blog, I intend to break down the RegExs in the MultiPaste.ahk script as much as possible to make them intelligible to the beginner. For now, you can look them up at the AutoHotkey Regular Expressions (RegEx) Quick Reference page.

UK Postal Codes

I know that I have a few readers in the UK who rarely use US Zipcodes, but the UK postal code RegEx looks crazy when it comes to validation:

^([A-Za-z][A-Ha-hK-Yk-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$

(The length of this code does not allow it to fit on a single line on this page—although it should remain intact if used in any script.)

However, since I only need to recognize a possible UK code and not determine its validity, I shortened the RegEx to:

\s([A-Za-z][A-Za-z]?\d\w?)

and inserted it into the RegExReplace() function:

Clipboard := RegExReplace(Clipboard, "\s([A-Za-z][A-Za-z]?\d\w?)", "`t$1")

This RegEx looks for a space preceding anything that might match the beginning of a UK postal code. Whether it’s a valid code or not is someone else’s problem. (More on this particular RegEx code in my next blog.)

Now, after copying a selected address with the CTRL+ALT+F key combination, I can paste pieces of a copied address into specific data fields by pressing the CTRL+ALT+WIN+W Hotkey combination, then hitting the number key corresponding to the appropriate item in the MsgBox.

Next time, I dig deeper into the Regular Expressions (RegEx) found in the MultiPaste.ahk script to explain in more detail the power a simple RegEx can add to your scripts.

Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)

jack

This post was proofread by Grammarly
(Any other mistakes are all mine.)

(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)

2 thoughts on “Parsing and Pasting One-Line Street Addresses (AutoHotkey Multi-Paste Trick)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s