String.replaceAll() weirdness?

By accident I found this oddity in replaceAll(). Is it a bug or a feature?

String str = "random|string";

System.out.println(str);

System.out.println(str.replaceAll("|", " "));

This code results in this output:

random|string

r a n d o m | s t r i n g

I know that I need to escape the vertical bar in order to replace the character correctly, that'snot the issue here.

What happens in the call to replaceAll and why doesn't it throw an exception? Somehing clearly gets replaced by a space but just an OR shouldn't be a correct regular expression, unless it's interpreted as "nothing OR nothing". If that's the case, what is this nothingness in between the characters and what else can be done with it?

Ove, slightly confused.

[790 byte] By [OveCarlssona] at [2007-10-2 10:33:33]
# 1

It's "nothing or nothing". I don't know how to define or explain it, but I imagine it's in the regex spec, and maybe in some of the tutorials.

[url=http://java.sun.com/docs/books/tutorial/extra/regex/index.html]Sun's Regular Expression Tutorial for Java[/url]

[url=http://www.regular-expressions.info/]Regular-Expressions.info[/url]

As for what else can be done with it, I don't know where it's useful, but you could use it to specify something like "A or B or nothing". I don't necessarily call that useful because you can do "(A|B)?" and get the same result, I think. There might be some other similar situation where it's not possible or maybe just messier to do it another way.

jverda at 2007-7-13 2:19:15 > top of Java-index,Java Essentials,Java Programming...