java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0
prathi suku
Greenhorn
Joined: Sep 14, 2009
Posts: 7
posted
0
Hi everyone
I am getting this error "java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0"
I am reading an input file which has data like
?xyz
In my code I am replacing the binary equivalent of ? that is "\u003F" with ""
text = text.replaceAll("\u003F","");
But I still get this error...
Any help is greatly appreciated
Thanks in advance.
The replaceAll() method takes a regex as the first parameter. And the "?" has special meaning in a regex. If you want it to behave as an literal question mark, then you will need to escape it.
Unicode characters are parsed and substituted before the source file is compiled. Your line of code is exactly the same as You need to escape the ? as Henry suggested. Keep in mind that the regular expression escape character has special meaning in Java Strings as well, so you need to escape that one as well.
Henry Wong wrote:The replaceAll() method takes a regex as the first parameter. And the "?" has special meaning in a regex. If you want it to behave as an literal question mark, then you will need to escape it.
By the way, there is now also a String.replace method that takes CharSequences and does not use regular expressions. Since String implements CharSequence it would be much easier to use that one:
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0