replaceAll(.....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sgxbytes
    New Member
    • Sep 2008
    • 25

    replaceAll(.....

    Hi,
    [code=java]
    String s = "hello in the (?hell?)";
    s = s.replaceAll("? ", "\"");
    [/code]
    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 .PatternSyntaxE xception: Dangling meta character '?' near index 0


    can anyone suggest me how to go ahead with this.

    Regards
    Raj
  • sgxbytes
    New Member
    • Sep 2008
    • 25

    #2
    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(“?(“)(“

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      I believe you need to escape the ? with a \. You would in Perl, at least, and I see no reason not to assume the same in Python.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sgxbytes

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

        t..

        Regards
        Raj
        Just use [CODE=java]yourString.repl aceAll("\\?", "\"");[/CODE]

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Check this page from the Sun Regular Expression Lesson. It shows you, that '?' is indeed a quantifier and therefore has to be escaped as shown in the previous posts.

          Greetings,
          Nepomuk

          Comment

          Working...