replacing..

R

raj

Hi,

String s = "hello in the (?hell?)";
s = s.replaceAll("?", "\"");

i hav a string s as shown above
i want to replace ? with "

the output should be like

hello in the ("hell")
i tried the using replaceAll replaceAll("(?", "\"");
but i am getting an error like

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


can anyone suggest me how to go ahead with this.

Regards
Raj
 
K

Knute Johnson

raj said:
Hi,

String s = "hello in the (?hell?)";
s = s.replaceAll("?", "\"");

i hav a string s as shown above
i want to replace ? with "

the output should be like

hello in the ("hell")
i tried the using replaceAll replaceAll("(?", "\"");
but i am getting an error like

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


can anyone suggest me how to go ahead with this.

Regards
Raj

public class test {
public static void main(String[] args) {
String s = "hello in the (?hell?)";
System.out.println(s.replaceAll("\\?","\""));
}
}
 
R

raj

raj said:
String s = "hello in the (?hell?)";
s = s.replaceAll("?", "\"");
i [sic] hav a string s as shown above
i [sic] want to replace ? with "
the output should be like
hello in the ("hell")
i [sic] tried the using replaceAll replaceAll("(?", "\"");
but i [sic] am getting an error like
java.util.regex.PatternSyntaxException: Dangling meta character '?'
near index 0

'?' is a significant character to a regular expression, as is '('.  Escape the
question mark.

(untested)
s = s.replaceAll("\\?", "\"");

Have you considered reading the Javadocs? (JAYF)
<http://java.sun.com/javase/6/docs/api/java/lang/String.html#replaceAl...)>
wherein they link you to an explanation of regular expressions:
<http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html#sum>

You should always check the Javadocs.  Did you?


hi,
Thanks for the reply

String s = "hello in the (?hell?)";

the output should be of this format

hello in the (“hell”)

its not double quote for hell

i tried this way for replacing the first (? with (“

s = s.replaceAll("\\(?" , "(“");

but i am getting o/p as


(“h(“e(“l(“l(“o(“ (“i(“n(“ (“t(“h(“e(“ (“(“?(“h(“e(“l(“l(“?(“)(“
 
R

raj

raj said:
String s = "hello in the (?hell?)";
the output should be of this format
hello in the (“hell”)

Those are not regular quotes.
its not double quote for hell
i [sic] tried this way for replacing the first (? with (“
s = s.replaceAll("\\(?" , "(“");
but i [sic] am getting o/p as
(“h(“e(“l(“l(“o(“ (“i(“n(“ (“t(“h(“e(“ (“(“?(“h(“e(“l(“l(“?(“)(“

Read the Javadocs.

You escaped the parenthesis, but not the question mark in the regular
expression.  You didn't even need the parenthesis in the regular expression.

Don't use wacky quote marks.

Read the Javadocs.

i hav gone through the javadocs

and tried this way which worked out for me ,


String s = "hello in the (?hell?) now going to (?heaven?) on the ?
way?)";


s = s.replace("(?" , "(“").replace("?)", "”)").replace("?", "“");


the output is

hello in the (“hell”) now going to (“heaven”) on the “way”)


i have replaced with the wacky quote marks as i need to compare this
string with database string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top