aspose file tools*
The moose likes Java in General and the fly likes RegEx Q - 1.4 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "RegEx Q - 1.4" Watch "RegEx Q - 1.4" New topic
Author

RegEx Q - 1.4

Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
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

Joined: Jun 27, 2002
Posts: 4118
tmp = tmp.replaceAll("[$]","\\\\\\$");

HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4


Java Regular Expressions
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Can you explain what each of the six backslashes is for?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
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

Joined: Jun 27, 2002
Posts: 4118

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

Joined: Jun 27, 2002
Posts: 4118
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

Joined: Nov 24, 2000
Posts: 776
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

Joined: Jun 27, 2002
Posts: 4118
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RegEx Q - 1.4