Strip HTML Tags from Text (AutoHotkey Quick Tip)

Use This AutoHotkey Trick to Remove HTML Code from Any Text

Last time in “Alternative Web Page HTML Download Techniques (AutoHotkey Tip),” I mentioned how I updated the GooglePhraseFix.ahk script by aaston86 to get it working again and make it a little more robust. The script uses a Google search page to autocorrect common expressions and people’s names. (It only works if Google thinks you may have made an error.)

For example, if you type “Ralph Nadal” the Spanish tennis player, selecting the name and using the CTRL+ALT+G Hotkey combination changes “Ralph Nadal” to “Rafael Nadal.” It only works for obvious possibilities, but may come in handy for correcting hard to remember spellings (i.e. “Jocavic” turns into “Djokovic”).

I added the phrase “Showing results for ” to the script as a search key in the Google results page. Google includes the phrase when it senses that you may have made a mistake. The original script used the StringReplace command to remove some HTML code and correct any apostrophes ('):

   StringReplace, clipboard, match2, <b><i>,, All
   StringReplace, clipboard, clipboard, </i></b>,, All
   StringReplace, clipboard, clipboard, ',', All

The StringReplace command can work for unchanging HTML tags but you need to add the command for each tag (or set of tags). By using the RegExReplace() function, you can remove all HTML code with one command.

HTML Tag Stripping Regular Expressions (RegEx) Using the RegExReplace() Function

The selected section of the Google page now includes a lot more HTML code than merely italics <i> and bold <b>. Using the following expression removes it all:

   var := RegExReplace(var,"<.+?>")

You don’t need to know anything about AutoHotkey Regular Expressions (RegEx) to use the above RegExReplace() function. The command removes all text found in var bounded by the arrow brackets (< … >).

Suppose you want to copy all the text from a Web page to a file. You could use the URLDownloadToFile command to copy the page source code, then execute the above RegExReplace() function to remove all of the HTML code. Only the plain text remains.

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.)


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.)

Find my AutoHotkey books at ComputorEdge E-Books!

Find quick-start AutoHotkey classes at “Robotic Desktop Automation with AutoHotkey“!

Leave a Reply

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

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

Google photo

You are commenting using your Google 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