The amazing adventures of Doug Hughes

Ever needed to have a regular expression in ColdFusion match multiple lines? I bet you thought it wasn’t possible. I sure did. Luckily, everyone’s favorite CF Junky, Jared, set me straight.

It turns, by default, in ColdFusion the ReFind and ReFindNoCase functions will not find matches which span line breaks. I killed this fly with an elephant swatter: I used java’s regex capabilities directly. What a pain.

Turns out that you can actually just prepend “(?m)” to your regular expression and viola, it works!

Turns out there’s a few of these types of things you can apply. For instance, you can use (?x) to ignore white space. Using (?i) will make the regex case insensitive, even if you’re using ReFind().

For a full list of these options see the docs at: http://livedocs.macromedia.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00000980.htm

Comments on: "Multiline Regular Expressions with ColdFusion" (3)

  1. Can’t say enough about a little application called RegexBuddy. Saves me every time.

    Like

  2. Seth Petry-Johnson said:

    The other neat thing about multi-line patterns with (?m) is that you can add comments to your regexp: anything at the end of a line and starting with “##” is ignored.

    This is great when you’re dealing with a complex pattern because it makes maintenance of the expression so much easier… I highly recommend documenting your expressions this way if they are anything non-trivial.

    Like

  3. Seth Petry-Johnson said:

    OK, so this is way late, but I’m an idiot. (?x) enables the end of line comments, not (?m). Doh!

    Like

Comments are closed.

Tag Cloud

%d bloggers like this: