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  |  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."


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 »
7,572 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 »
7,572 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 »
165 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 »
7,572 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
Mar 21, 2010 at 8:57 PM
The Bourne Ultimatum Starring Matt Damon And Julia Stiles
late to the party, but my observation is this: rewatch carefully for the platonic nature of the relationship between nicki and jason. she never flirts with him. he never comes on to her. they alway ... read »
Mar 21, 2010 at 7:40 PM
Is Simulating User-Input Events With jQuery Ever A Good Idea?
A couple of things. One you embed the initial state of of more-info in the CSS. IMHO, that behavior should be in jQuery: moreInfo.hide(); It shows that the behavior your toggling and closing is mor ... read »
Mar 21, 2010 at 3:59 PM
Exploring ColdFusion Component Runtime Class Properties And Serialization
@Elliott, according to Ben's experiment, serializeJSON() doesn't access the private data by default - it doesn't even access the getHair() method - so trying to clone a Girl.cfc via serializeJSON/des ... read »
Mar 21, 2010 at 3:49 PM
Ask Ben: Javascript String Replace Method
I'm confused a bit by what you are asking, but if had this sentence: The color, red, is in the style statement; style: red;. and wanted to remove all or change all of the commas, colons, and semi-c ... read »
Mar 21, 2010 at 3:13 PM
Ask Ben: Javascript String Replace Method
I am trying to make a java program to count the number of times that these punctuation marks occur in a body of text: , : ; . ! - ' " ? / \ I am using this piece to ferret out the commas: numcommas ... read »
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »