wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes replaceall() doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replaceall() doubt" Watch "replaceall() doubt" New topic
Author

replaceall() doubt

Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
We all know what ReplaceAll() function does in Java.

Check this simple code :



This simple code as expected gives an O/P of Hello@World

But consider the following code:



The o/p is
@@@@@@@@@@@

Some one please explain this unusual behaviour


SCJP 5.0 77%
Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
Hi,
Got this from javadocs


replaceAll

public String replaceAll(String regex,
String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.


In ur case, u r doing


"." regular expression matches "anything" so it replaces all characters with "@".

-Vinayak


"I can resist everything except temptation"
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9337
    
110

The replaceAll method considers the first parameter to be a regular expression, and as per the javadoc:
Replaces each substring of this string that matches the given regular expression with the given replacement.


And the "." represents:
Any character (may or may not match line terminators)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

So, "Hello.World".replaceAll(".","@"); means replace all characters in the string with @.


[My Blog] [JavaRanch Journal]
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
Thanks for all those replies. But why do i get error in this case:

O/P:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at Me.main(Me.java:11)

Vinayak patil
Ranch Hand

Joined: Aug 30, 2005
Posts: 67
Another observation related to this:



gives me an output of Hello@World

I think it has to do something with the foll , though I am not sure how.

From javadocs


Backslashes within string literals in Java source code are interpreted as required by the Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

"\\" is a Java string containing a single '\' character. A single '\' character is not a valid regex. To match a backslash, you need a regex with two backslashes. To create such a string in Java, you need to escape them both -- i.e., you need to write "\\\\" to match '\'!


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: replaceall() doubt
 
Similar Threads
increment operator
For Rahul Mahindrakar
Java file is compiling but not running.
how many objects created
learning polymorphism and construction