Generating regexes and Gmail filters

Online regex generator a useful tool

Before I launch into my main thrust (a word that must be pronounced with a rolling "r") I have to direct you to a work of near genius, txt2re, an online regular expression code generator.

If you aren't au fait with regular expressions (also called regexes or regexps), they are formal descriptions of searches to be conducted on sequences of characters (or "strings") by a regular expression processor, that is, a program designed to process "regexes" (see the Wikipedia entry on regular expressions).

Regex is useful for jobs such as mining server logs and searching data files and txt2re makes generating code in Perl, PHP, Python, Java, Javascript, ColdFusion, C, C++, Ruby, VB, VBScript, J#.net, C#.net, C++.net or VB.net that perform these searches incredibly easy.

To use txt2re you give the service an example string and it shows you the substrings it recognizes and lets you select which ones you want to include in the output.

I did, however, say "near genius" as txt2re seems to have a bug that means the service doesn't always identify all of the "findable" substrings correctly. I was using txt2re to generate JavaScript code based on the following example entry in an Apache server access log:

192.168.10.11 - bob [16/Mar/2009:13:14:15 -0800] "GET /gibbs.gif HTTP/1.0" 200 5648

Txt2re failed to offer to treat the last digits in the string that show the data length as an integer -- it only offered them as four individual digits (see here), which would be useless if the data length was five digits long.

The solution was, oddly enough, to change the IP address in the example string to 1.1.1.1 and voila! I got the code I needed (see here). Despite this bug, the concept is way cool and a little creative tweaking of either your example or the generated code will get you the code for exactly the regex search you need. (Here is a telephone number parser in JavaScript I created using txt2re).

Join the newsletter!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection

Tags Gmailregular expressions

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Mark Gibbs

Network World
Show Comments

Father’s Day Gift Guide

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

David Coyle

Brother PocketJet PJ-773 A4 Portable Thermal Printer

I rate the printer as a 5 out of 5 stars as it has been able to fit seamlessly into my busy and mobile lifestyle.

Kurt Hegetschweiler

Brother PocketJet PJ-773 A4 Portable Thermal Printer

It’s perfect for mobile workers. Just take it out — it’s small enough to sit anywhere — turn it on, load a sheet of paper, and start printing.

Matthew Stivala

HP OfficeJet 250 Mobile Printer

The HP OfficeJet 250 Mobile Printer is a great device that fits perfectly into my fast paced and mobile lifestyle. My first impression of the printer itself was how incredibly compact and sleek the device was.

Armand Abogado

HP OfficeJet 250 Mobile Printer

Wireless printing from my iPhone was also a handy feature, the whole experience was quick and seamless with no setup requirements - accessed through the default iOS printing menu options.

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?