Issues with Back Slashes (\) And Java's Matcher::AppendReplacement() Method

Posted September 10, 2006 at 11:23 AM by Ben Nadel

Tags: ColdFusion

I just talked about the problems using $ signs and AppendReplacement(), but ironically, that post in and of itself, broke my code. Since my code had a slot of back slashes literals in it, the AppendReplacement() method was trying to evaluate them as esacpING characters, NOT escapED characters.

To quickly fix this, I updated the code:

  • // Loop over the matcher while we still have matches.
  • while ( LOCAL.Matcher.Find() ){
  •  
  • // Get the sample.
  • LOCAL.Sample = LOCAL.Matcher.Group();
  •  
  • ... MANIPULATION of Group Data ...
  •  
  • // Add the sample to the buffer. When appending the
  • // replacement, be sure to escape any $ signs with
  • // literal $ signs so that they are not evaluated are
  • // regular expression groups. Also, escape the \ so
  • // that they are not evaluated as escaping characters.
  • LOCAL.Matcher.AppendReplacement(
  • LOCAL.Results,
  • LOCAL.Sample.ReplaceAll( "\\", "\\\\" ).ReplaceAll( "\$", "\\\$" )
  • );
  •  
  • }

This escapes all back slash literals in my string before it goes through and escapes all dollar sign literals.




Reader Comments

Apr 19, 2010 at 4:45 PM // reply »
3 Comments

It should be, in fact, replaceAll( "\\\\", "\\\\\\\\" )


Apr 19, 2010 at 9:10 PM // reply »
10,743 Comments

@Vlad,

I am not sure that I understand what you mean? I have tested my code and it works fine.


Apr 20, 2010 at 9:45 AM // reply »
3 Comments

May be I'm missing something, but it can't be compiled as replaceAll("\\","\\\\"), it would give:
<code>
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
</code>
It makes sense to me, as you have to escape it twice: once for string and once for regex.
Not sure how it works for you.


Apr 20, 2010 at 9:57 AM // reply »
10,743 Comments

@Vlad,

Ahh, I see what's going on here. This is not Java code. This is ColdFusion code (which automatically compiles down to Java). In ColdFusion, there are no special string characters; as such, I only have to escape the value for the RegularExpression, not for the string itself.

Simple miscommunication :)


Apr 20, 2010 at 10:14 AM // reply »
3 Comments

My bad, I missed that.
I see I'm not the first one to make that mistake in your blog :)
Your pages come up nicely when querying google for java issues
Thanks


Apr 21, 2010 at 9:31 AM // reply »
10,743 Comments

@Vlad,

No worries my friend. Heck, I didn't even mention the language in the blog post. That's pretty exciting for me, though, that this kind of stuff comes up for Java searches as well :)


Apr 30, 2010 at 5:06 PM // reply »
1 Comments

Not to nitpick, but wouldn't '#' be considered a special string character in ColdFusion?


May 10, 2010 at 10:21 PM // reply »
10,743 Comments

@KingErroneous,

Yeah, # is definitely a special character in ColdFusion, but only in a string that is going to be evaluated. Having a # in a piece of string data (such as that read from a file or in a FORM post), there is not special about it. And, of course, to escape it in ColdFusion, you need to use a double-pound, ##, rather than a back-slash as you might in other languages.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2012 at 1:58 AM
Updated: Converting A ColdFusion Query To CSV Using QueryToCSV()
Hi Ben, why do you need to have so many double quotes when adding the field and field name to the row data? ----------------------------------------- <cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = ... read »
AXL
May 21, 2012 at 1:24 AM
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
@Mounir, Open your lower case URL Rewrite rule and add the following condition. Condition input: {REQUEST_URI} Check if input string: Does Not Match the Pattern Pattern: ^/CFFileServlet/_cf_ca ... read »
May 20, 2012 at 4:28 AM
Understanding The Complex And Circular Relationships Between Objects In JavaScript
@Will Vaughn I tried your javascript example but got this error:- foo.print is not a function ... read »
May 19, 2012 at 5:37 AM
A Graphical Explanation Of Javascript Closures In A jQuery Context
Thanks for this article, but I fear you missed an important point. If variables in the outer context change, these changes affect the inner anonymous functions as well. That means: if you change the ... read »
May 18, 2012 at 3:39 PM
Parsing CSV Data With An Input Stream And A Finite State Machine
Can you use file upload button with this? and read live? or does the file have to already be on the server saved? ... read »
May 18, 2012 at 1:06 AM
VIRGO (Aug. 23-Sept. 22): Dead On The Money!
A friend of mine and I were arguing about astrology and she told me that he believes in astrology. She hasn't provided me with any evidence that the belief makes any sense to me. She she been telling ... read »
May 17, 2012 at 11:32 PM
Using ColdFusion to Handle 404 Errors (Page Not Found) On Development Server
Very easy the configuration. I read a lot pages and I can't find the solution. I open the administrator and change this Administrator/server settings/Error Handlers/Missing Template Handler and p ... read »
May 17, 2012 at 3:13 PM
LOCAL Variables Scope Conflicts With ColdFusion Query of Queries
I never cease to be amazed that almost EVERY random CF issue I come across lands me on your site. Thank you for documenting your findings for the world. ... read »