• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0

 
prathi suku
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21490
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

text = text.replaceAll("\\?","");

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
prathi suku
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

text = text.replaceAll("\\?","");

Henry



Hi Henry
I tried and that didn't work either.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Didn't work" in what way?
 
prathi suku
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used the code

text = text.replaceAll("\\?","");
instead of
text = text.replaceAll("\u003F","");

but I am still getting the error "java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0"
 
prathi suku
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry .

The code is working fine now

I forgot to comment the line
text=text.replaceAll("\u003F","")

before adding the new code.
Really sorry for the miscommunication.

Many thanks for all your help everyone
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic