*
The moose likes Beginning Java and the fly likes java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.util.regex.PatternSyntaxException: Dangling meta character Watch "java.util.regex.PatternSyntaxException: Dangling meta character New topic
Author

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

prathi suku
Greenhorn

Joined: Sep 14, 2009
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18542
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
prathi suku
Greenhorn

Joined: Sep 14, 2009
Posts: 7
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

Joined: Sep 29, 2008
Posts: 12617

"Didn't work" in what way?
prathi suku
Greenhorn

Joined: Sep 14, 2009
Posts: 7
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

Joined: Sep 14, 2009
Posts: 7
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0
 
Similar Threads
replace String - dangling meta character
How to replace "?" in a string
replaceAll("+"," ")
String "contains" equivalent - But should look for case-in-sensitive string
replace String - dangling meta character