Java + regular expressions + $ = problem

Discussion in 'OT Technology' started by Astro, Oct 22, 2003.

  1. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    So far I've been able to work around this "Java feature" but now I just can't. Here's the problem:

    Code:
    Pattern patt = Pattern.compile("\\Q<!--%" + key + "%-->\\E");
    Matcher matcher = patt.matcher(play_content);
    play_content = matcher.replaceAll(value);
    
    Assume "key", "value", and "play_content" are strings.

    I know "$" is a regular expression prefix to a regular expression token (like $1, $2, etc which match up with the pattern matches specified in the regular expression). WHY [insert potty language] IS JAVA HANGING UP ON THE $ IN "play_content" ??

    It gave me all sorts of problems when converting over from PHP - it didn't like the dollar sign to be in the token identifiers so I swapped them out with "%" which has been working. Now when play_content has a dollar sign - regardless of where, I get the following error:

    Line 101 is the last line found in the code I provided above.

    From the looks of it, I have to peform a replaceAll() on play_content - but is it a matter of playing the "How many back slashes do I need to escape this character?" game (which I swore I played before and last every round), or do I have to get medieval and replace the $ with some odd-ball character sequence and then swap the sequence out with the $ when done?

    This has me uber-frustrated...
     
  2. crotchfruit

    crotchfruit Guest

    the replaceAll() documentation says,

    "Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string."

    looks like it calls appendReplacement when it sees a $.. here is some documentation from that method,

    "The replacement string may contain references to subsequences captured during the previous match: Each occurrence of $g will be replaced by the result of evaluating group(g). The first number after the $ is always treated as part of the group reference. Subsequent numbers are incorporated into g if they would form a legal group reference. Only the numerals '0' through '9' are considered as potential components of the group reference. If the second group matched the string "foo", for example, then passing the replacement string "$2bar" would cause "foobar" to be appended to the string buffer. A dollar sign ($) may be included as a literal in the replacement string by preceding it with a backslash (\$). "

    so maybe you should go through play_content and insert a backslash before any $ signs to fix the problem :p
     
  3. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    I could have sworn I did this when I had the dollar sign in my tags (couldn't get it to work which was why I switched to the percent sign). I'm going to revisit this Thurs morning and see whats up. I'll post the results here.
     
  4. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    As of this moment:

    Code:
    String play_content = content.replaceAll("\\x24", "dollar sign");
    
    String play_content = content.replaceAll("\\x0024", "dollar sign");
    
    String play_content = content.replaceAll("\\$", "dollar sign");
    
    String play_content = content.replaceAll("$", "dollar sign");
    
    Those do NOT work. WTF!?
     
  5. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    :uh:

    I was working on the wrong variable... ugh.

    Here's the code that made it work (when applied to the correct variable):

    Code:
    [correct variable].replaceAll("\\$", "\\\\\\$")
    
     
  6. crotchfruit

    crotchfruit Guest

    most obfuscated functions evar.

    btw astro, check pms.
     
  7. Astro

    Astro Code Monkey

    Joined:
    Mar 18, 2000
    Messages:
    2,047
    Likes Received:
    0
    Location:
    Cleveland Ohio
    Yes and did already. Will be acting on your PM probably on Fri otherwise Sun. I'm just on to check in.
     
  8. aphoric

    aphoric Even if god did exist, it would be necessary to ab

    Joined:
    Aug 29, 2003
    Messages:
    918
    Likes Received:
    0
    Location:
    Leaving Afghanistan
    wait until tiger (JDK 1.5) comes out...
     

Share This Page