Regular expressions are great, but they're a bit unwieldy. There's a well-known quote about regular expressions that goes like this: Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems.

I think of it whenever I try to tackle something using regular expressions, but most of the time, I spend more time wrestling with the regular expression engine than I do with the actual regular expression itself. I thought that the chainable operation of jQuery would work pretty well for regular expressions, so I gave it a shot. textMonster lets you chain as many regular expressions as you want together and iterate over their results in a straightforward manner.

Usage

Note: exact usage varies by language, but this is the broad overview, presented using the JavaScript version.

textMonster operates on strings. Given a string and a regular expression, each of the matches from that string are held in a textMonster result object. You can iterate through the results, or you can chain another regular expression on to the end. The result of that second regular expression will contain all of the matches from all of the previous matches, and there's no limit to how many regular expressions you can chain together.

var tM = "The first number is foo123, the second number is bar456, and the third number is baz789.".textMonster("[a-z]+[0-9]+").textMonster("[0-9]+");

In the code snippet above, the first regular expression will return "foo123", "bar456", and "baz789". The second regular expression will return "123", "456", and "789".

JavaScript

Download textmonster.js

Python

The Python version works a bit different, since the base string class can't be extended. Instead, pass the string in to the textMonster function, and after that, things work pretty much the same way:

tM = textMonster("The first number is foo123, the second number is bar456, and the third number is baz789.").textMonster("[a-z]+[0-9]+").textMonster("[0-9]+");

Download textmonster.py

Ruby

Sean Smith has made a Ruby version of textMonster. Ruby's object system is fantastic, and this is definitely the most elegant solution. The textMonster functions are added to the base String and Arrray classes.

tM = "The first number is foo123, the second number is bar456, and the third number is baz789.".textMonster('[a-z0-9]+').textMonster('[0-9]+')

Download textmonster.rb

Feedback

If you have any comments or suggestions, I'd love to hear them -- drop me a line.