Matching Multi-Line Regular Expression Patterns In MULTILINE Mode (?m)

Posted November 5, 2009 at 2:04 PM

Tags: ColdFusion

This morning, I was (and am still) having a problem getting some MULTILINE regular expression patterns to match properly. As such, I wanted to put a quick blog post together as a sanity check for myself. As I have blogged about before, when a Java regular expression is running in multiline mode (as denoted by the "?m" flag), the "^" and "$" expressions match the line start and line terminator (respectively) rather than the start and end of the source string. This allows us to do line-level pattern matching.

This is really useful; but, when we are running our regular expressions in multiline mode, we have to be aware that the new line and carriage return data is not matched inside of the ^ and $ expressions. As such, if we want to match a pattern across multiple lines in multiline mode, we have to define the new line and carriage return expressions explicitly in our pattern. To see this, take a look at the following demo:

 Launch code in new window » Download code as text file »

  • <!--- Store target data. --->
  • <cfsavecontent variable="data">
  • AAAAAAAAAABBBBBBBBBB
  • CCCCCCCCCCDDDDDDDDDD
  • EEEEEEEEEEFFFFFFFFFF
  • GGGGGGGGGGHHHHHHHHHH
  • </cfsavecontent>
  •  
  • <!---
  • Create the Java pattern. Note that we are using the
  • MULTILINE mode flag; this will allow ^ and $ to match the
  • line delimiters.
  • --->
  • <cfset pattern = createObject( "java", "java.util.regex.Pattern" )
  • .compile(
  • javaCast(
  • "string",
  • (
  • "(?m)D.++$" &
  • "(\r\n?|\n)" &
  • "^.*+$" &
  • "(\r\n?|\n)" &
  • "^G"
  • ))
  • )
  • />
  •  
  • <!--- Get the matcher for our target text. --->
  • <cfset matcher = pattern.matcher(
  • javaCast( "string", trim( data ) )
  • ) />
  •  
  • <!--- Move to the first match. --->
  • <cfset matcher.find() />
  •  
  • <!--- Output the first match. --->
  • [#matcher.group()#]

Here, we start out matching the letter "D" and then everything until the end of the line (remember that in Java, the "dot" does not match line terminators until DOTALL mode [aka. single-line mode] is turned on using ?s). We then add the line terminators to the pattern. We then match the entire next line and match its line terminators. We then match the line start followed by "G". When we run this code, we get the followingg output:

[DDDDDDDDDD
EEEEEEEEEEFFFFFFFFFF
G]

Anyway, nothing revolutionary going on here; like I said above, I mostly put this post together as a sanity check for myself to make sure that I really understood what was going on in Java's multiline mode.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Nov 5, 2009 at 2:43 PM // reply »
30 Comments

Did you look to see if it would return the end of line character if in single line mode? I have an idea how I would go about it and may try it later.


Nov 5, 2009 at 3:34 PM // reply »
2 Comments

The way that expression is written, you know you'll have exactly one line between the DDD line and the line that starts with a G. Was that your intention? If you add a line or even a blank line no match is found. If you need more flexibility may I suggest something like:

"(?m)D+$(\r\n?|\n)((^.*$)(\r\n?|\n))+^G"

a match is found even with:

<cfsavecontent variable="data">
AAAAAAAAAABBBBBBBBBB
CCCCCCCCCCDDDDDDDDDD
EEEEEEEEEEFFFFFFFFFF

T

GGGGGGGGGGHHHHHHHHHH
</cfsavecontent>


Nov 5, 2009 at 3:48 PM // reply »
6,522 Comments

@Daniel,

Hmm, I think it would. When in single line mode, the dot will match line terminators, which means that .++ should possessively match ALL characters until it hits the end of the string.

@Travis,

Yes, I mean to only match a single line between the D and G (that was more along the lines of the use case I was trying to debug). But yes, the way you have it would be more flexible.


Nov 8, 2009 at 10:06 AM // reply »
2 Comments

This is a good web log and i like it.
It will help people to learn ColdFusion.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 24, 2009 at 3:20 AM
Ask Ben: Tracking File Downloads In ColdFusion
Could u pls let me know solution of this problem ... read »
Nov 24, 2009 at 3:18 AM
Ask Ben: Tracking File Downloads In ColdFusion
i m facing a issue with cflocation , if file name is same for 3-4 entries in same page, nd user try to download, then it display the same file every time ... read »
Nov 24, 2009 at 2:05 AM
Ask Ben: Overriding Core jQuery Methods
But wan't it create problems when we upgrade to more progressive version of the plugin? We would have to analyse all our changes from the beggining before changing the plugin , and if we are talking ... read »
Nov 24, 2009 at 1:46 AM
Ask Ben: Iterating Over An Array With jQuery
Thanks for this! ... read »
Nov 24, 2009 at 1:14 AM
CFContent-Variable Response vs. Standard Output Response
I always thought you took the streamy approach because of some kind of performance gains or reduced server overhead you secretly knew about. Excited for the follow up. ... read »
Nov 24, 2009 at 1:08 AM
Colin Moock's ActionScript 3.0 Event
UGG Women's Metallic Classic UGG Women's Sandra UGG Australia Sandals UGG Classic Mini Boots UGG Lo Pro Classic Tall UGG Nightfall 5359 UGG Roxy Boots UGG Sundance II 5325 UGG Ultra Short UGG Ultra T ... read »
Nov 23, 2009 at 9:27 PM
Using The Apple iPod Shuffle Without iTunes
Does this rename your music and place it automatically? I was just thinking that it would be hard to find and delete songs if it did. ... read »
Nov 23, 2009 at 9:19 PM
Using Contextual SMS Short Code Messages With TextMarks And ColdFusion
Just found this. Great job Ben. I'm new to SMS stuff in general. I'm really interested in how the phone number is matched up to the unique id. Is that a CF thing? Where is that being handled? Maybe ... read »