Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RegEx Q - 1.4

 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys - Since I've been beating my head against this for an hour or so with no results, I'll ask here:
-Given: an input String with some ($) dollar-signs in it, arbitrary positions, could be at beginning, middle, end.
-Desired: an output String with each ($) dollar-sign replaced by (\$) back-slash+dollar-sign.
-Methodology: using JDK1.4 RE's
I thought this would be just a 2-3 lines of code, but have been unable to come up with anything that works.
Any help appreciated.
Regards, Guy
Later - Here is a code example:
-----------
// This works, and does what I am asking for.
Pattern p = Pattern.compile("X");
Matcher m = p.matcher("<X>Somewhere the concept of <X> is found.<X>");
System.out.println("OUT: " + m.replaceAll("ZZTOP"));
// This works too! Half way there!
p = Pattern.compile("\\$");
m = p.matcher("<$>Somewhere the concept of <$> is found.<$>");
System.out.println("OUT: " + m.replaceAll("JBUFFET"));
// None of these work!?!?!?!
p = Pattern.compile("\\$");
m = p.matcher("<$>Somewhere the concept of <$> is found.<$>");
// Note: if the parameter for 'replaceAll' is -
// "\\$" an unchanged String is returned.
// "\\\$" does not compile.
// "\\\\$" compiles, but an exception is generated.
System.out.println("OUT: " + m.replaceAll("\\\\$"));
-----------
G.
[ September 24, 2002: Message edited by: Guy Allard ]
[ September 24, 2002: Message edited by: Guy Allard ]
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tmp = tmp.replaceAll("[$]","\\\\\\$");

HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain what each of the six backslashes is for?
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max - Yes, that works. However, it does not meet my specs.
'tmp' is a String, not a Matcher.
Regards, Guy
Later - OK Max, thanks, you got me thinking in the right direction.
Works if your replacement pattern is used with m.replaceAll, e.g.
System.out.println("OUT= " + m.replaceAll("\\\\\\$");
G.
[ September 25, 2002: Message edited by: Guy Allard ]
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm not using a matcher explicitly, because the problem doesn't require that degree of complexity. However, I am using regex. The new String method 'replaceAll" internally uses

If you feel better about it, please feel free to use the above.
HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
[ September 25, 2002: Message edited by: Max Habibi ]
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:
Can you explain what each of the six backslashes is for?


Hi Ron,
Happy to.
Consider the String
tmp = tmp.replaceAll("[$]","\\\\\\$");
The String you really want is '\' + '$'. But '$' is a meta character, so you delimit it with a '\'.
Now your string is
'\' + '\$'. But the first '\' needs to be delimited also, because it's also a meta character, so you get
'\\' + '\$'.
Now, Because you're passing all of this to a String, you need to delimit the '\' characters once again, so you double the '\' characters
thus,
'\\' becomes '\\\\' and
'\$' becomes '\\$'.
Concatanate the two, and we have
'\\\\\\$'
HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max - I edited my post above to indicate you gave the basic idea of how to proceed using Pattern and Matcher objects. Thanks again.
I had been playing with this on Linux, and the patterns work basically the same way, e.g:
s="\$aaa\$bbb\$"
echo $s | sed -e "s/\\\$/\\\\\\$/g"
Regards, Guy
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guy Allard:
Hi Max - I edited my post above to indicate you gave the basic idea of how to proceed using Pattern and Matcher objects. Thanks again.
I had been playing with this on Linux, and the patterns work basically the same way, e.g:
s="\$aaa\$bbb\$"
echo $s | sed -e "s/\\\$/\\\\\\$/g"
Regards, Guy

Glad to be of service.
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic