File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings replaceAll()

 
K Riaz
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it works just like replace() except it takes a regular expression instead of a single character as the filter.
 
K Riaz
Ranch Hand
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of expression? And why do some take double slashes as an argument?

Like aString.replaceAll("\\", "z");
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And for RegExp specifically in Java, our own Max Habibi has written a very nice book.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
argh. you're right, Layne, i was overlooking the syntax of Java itself. my bad.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic