ColdFusion Regular Expressions Match New Line Using "."

Posted November 28, 2006 at 5:35 PM

Tags: ColdFusion

Minor note here, but over on CF-Talk, Rick Root just pointed out a flaw in my understanding of ColdFusion regular expressions. I was under the impression that the period "." would NOT match the new line character or the carriage return character in a target string. I think this is traditionally how regular expressions work (or maybe not). It turns out, that this is dead wrong. The "." character DOES match new lines and carriage returns. I am not sure if this changed in MX(?7) or if I just never learned in properly.

Thanks Rick!

My test:

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

  • <cfsavecontent variable="strText">
  • <a href="http://www.google.com"
  • target="_blank"
  • >Google</a>
  • </cfsavecontent>
  •  
  • <!--- Replace tags. --->
  • #REReplaceNoCase( strText, "</?[a-z].*?>", "", "ALL" )#

Download Code Snippet ZIP File

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




Reader Comments

Nov 29, 2006 at 1:28 AM // reply »
10 Comments

Yep, this has always been the case. From "Ben Forta's Advanced CFMX Application Development" (3rd ed.);

"It's important to understand that in ColdFusion MX, the dot character always matches newlines, which is not always the case with Perl."


Adam Cameron
Nov 29, 2006 at 5:01 AM // reply »
64 Comments

Good to know this (despite the fact it says it clearly in the docs, right back to CF5... who RTFMs? ;-)

I just ass-u-me`d that CF followed the practice set by PERL and Java. No. That would be too sensible.

Cheers for the heads-up.

--
Adam


Nov 29, 2006 at 9:21 AM // reply »
6,371 Comments

I think the problem is that I read the text book and learned a lot about ColdFusion before I even learned how to use Regular Expressions. So, by the time I knew what they were and how to apply them, I was no longer learning from the documentation... I was learning from google'd examples of regular expressions.


Nov 30, 2006 at 9:38 AM // reply »
38 Comments

Other's have said this, but Perl does it the way you stated, Ben. From the Perl docs: ". matches any single character except a newline". And ColdFusion regular expressions are based on the Perl syntax, but apparently they took liberties. :)


Dec 19, 2006 at 10:30 AM // reply »
95 Comments

I just found out that you can also remove new lines by doing:

<pre>
<cfsavecontent variable="testString">
<a href="http://www.google.com"
target="_blank"
>Google</a>
</cfsavecontent>

<cfoutput>#testString.replaceall("\r\n", "")#</cfoutput>
</pre>


Dec 19, 2006 at 2:14 PM // reply »
6,371 Comments

Boyan, excellent point. Just a follow up, that would only work if you don't mind losing other line breaks (outside of tags).


Feb 2, 2007 at 10:19 PM // reply »
164 Comments

It hurts my feelings tremendously that ColdFusion does this. I hate using [^\r\n] for staying within lines. And I typically use [\S\s] to match any character if the regex library I'm using doesn't support an explicit "dot matches newline" option. Semi-related sidenote: Non-negated \r\n pairs are unreliable in JavaScript, since Firefox uses only linefeeds (\n) to separate lines in <textarea>s. Instead, I typically use \r?\n, which works cross-browser.


Feb 3, 2007 at 9:11 AM // reply »
6,371 Comments

Yeah, I have run into that \r\n thing in Javascript before. It was driving me CRAZY! Cause I was outputting the ASCII values in ColdFusion to debug and sure enough it was there, but then in Javascript when I was testing for it, NOT there. That was like 30 minutes of my life I won't get back :)


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »