• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

replaceall() doubt

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 '\'!
reply
    Bookmark Topic Watch Topic
  • New Topic