File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replaceall() doubt

 
Satish Kota
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vinayak patil
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 @.
 
Satish Kota
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"\\" 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 '\'!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic