Java RegEx - Illegal character dot

String filter = "a-zA-Z0-9äöüÄÖÜß-\\.";
"^[^" + filter + "]*$", ""

inputtext.replaceAll("^[^" + filter + "]*$", "");

This Java RegEx filter should remove all characters from inputtext except the characters in filter, but I'm getting an exception that says that the dot is not allowed. I already escaped the dot with two slashes. What's wrong?

Answers


Move the - to the end:

String filter = "a-zA-Z0-9äöüÄÖÜß.-";

A dash indicates a character range and a dot is not a valid end of a range. You also do not want to define a range there. Moving it to the end solves the problem.

Btw. if you want to remove all characters that do not match your filter you need to change the replace:

inputtext = inputtext.replaceAll("[^" + filter + "]", "");

the way you have it, it will only replace if all characters are not in that class.


Need Your Help

Intel C++ compiler gives “offload constructs are not supported on this platform” error

c++ intel-mic cilk-plus xeon-phi intel-c++

When compiling a basic code segment on windows (using visual studio) that uses the _Cilk_offload keyword, the compiler throws error : offload constructs are not supported on this platform on all li...

iPhone Application crashes in “AdHoc” mode

iphone objective-c cocoa-touch build-process

i got a little application which is working very well with my "debug" configuration on my phone and in simulator.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.