File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Strings replaceAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings replaceAll()" Watch "Strings replaceAll()" New topic
Author

Strings replaceAll()

K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
Can someone explain exactly how this method works? The Java API does not give much away and I don't understand the "regex" argument. An example would be great, as I am unfamilar with pattern matching.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
it works just like replace() except it takes a regular expression instead of a single character as the filter.


42
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
What kind of expression? And why do some take double slashes as an argument?

Like aString.replaceAll("\\", "z");
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Sun has a tutorial on regular expressions, which I believe started either with Perl or C.

Usually, the double slash as you've shown is really a single slash; the first slash is "escaping" the second one. This is done because a single slash indicates an escape sequence, which is a special series of characters. For example, "\n" indicates a new line, "\t" indicates a tab, and so on. If you just had "\" the compiler wouldn't know what to do with that. Now, if you wanted to look for a backslash followed by an 'n', and used "\n", the expression would search for a newline character. To search for a backslash followed by an 'n', you would need to write "\\n".


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Kashif Riaz:
What kind of expression? And why do some take double slashes as an argument?

Like aString.replaceAll("\\", "z");


Regular expressions (or "regex" for short) are a "basic" type of language. They are typically used for pattern matching. As mentioned above, there are a lot of tutorials on the Web about them. For information specific to Java, you should google for Sun's regex tutorial. Google should also turn up more general information about regular expressions because they are not specific to Java.

Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Jeff Bosch:
Sun has a tutorial on regular expressions, which I believe started either with Perl or C.
...

C does not have any built-in support for regex's. They are quite common in Perl, but I their origin goes further back to Unix tools such as grep and awk. I don't know for certain the first tools or programming languages that implemented them, but they've been around for a long time. I'm sure a Google could enlighten the topic for anyone that is interested.

Layne
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
True, but UNIX, grep and awk were all written in C. So, even though C didn't have support for regex, they "came from the C". (Sorry for the pun. I've been staring at a monitor for too many hours!)
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
On which occasion would you do the following:



i.e. why would you replace a dollar in a string with a "\$"?
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
Originally posted by Kashif Riaz:


i.e. why would you replace a dollar in a string with a "\$"?


in regular expressions, $ stands for end of line. so, the above replacement is inserting a backslash at the very end of each line.

if you want to learn all about regexes, there's a book you can buy about them. (no, i do not work for the O'Reilly publishing company, i'm just a satisfied customer of theirs.)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
And for RegExp specifically in Java, our own Max Habibi has written a very nice book.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by M Beck:


i.e. why would you replace a dollar in a string with a "\$"?<hr></blockquote>

in regular expressions, $ stands for end of line. so, the above replacement is inserting a backslash at the very end of each line.

if you want to learn all about regexes, there's a book you can buy about them. (no, i do not work for the O'Reilly publishing company, i'm just a satisfied customer of theirs.)[/QB]

If "$" represents the end of the line (as an anchor, correct?), then "\$" reperesents a literal dollar sign. Since the Java compiler needs the back-slash escaped, then "\\$" becomes "\$" when the regex parser sees it. This in turn becomes just a literal "$". So as I see it, the above code is adding a dollar sign (not a back-slash) at the end of each line.

Layne
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
argh. you're right, Layne, i was overlooking the syntax of Java itself. my bad.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Just last night I wanted to change all back slashes to forward slashes. An argument of "\" is bad Java syntax cause it has to escape something, so I used "\\". That made a single slash which bad RegEx syntax cause it has to escape something, so I needed "\\\\". It's all good and I'm sure I'll get used to it one day, but it's a bit strange, ain't it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
That made a single slash which bad RegEx syntax cause it has to escape something, so I needed "\\\\". It's all good and I'm sure I'll get used to it one day, but it's a bit strange, ain't it.


Try to write a replaceAll statement that replaces all backslashes with double backslashes. It nearly got me crying - only the unit tests saved my sanity... :roll:


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
wood burning stoves
 
subject: Strings replaceAll()