replacing..

Discussion in 'Java' started by raj, Oct 16, 2008.

  1. raj

    raj Guest

    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
    raj, Oct 16, 2008
    #1
    1. Advertising

  2. raj wrote:
    > 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("\\?","\""));
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Oct 16, 2008
    #2
    1. Advertising

  3. raj

    raj Guest

    On Oct 16, 11:14 am, Lew <> wrote:
    > raj wrote:
    > > Hi,

    >
    > > 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?
    >
    > --
    > Lew- Hide quoted text -
    >
    > - Show quoted text -



    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(“?(“)(“
    raj, Oct 16, 2008
    #3
  4. raj

    raj Guest

    On Oct 16, 11:40 am, Lew <> wrote:
    > Lew wrote:
    > >> '?' 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?

    > raj wrote:
    > > 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.
    >
    > --
    > Lew- Hide quoted text -
    >
    > - Show quoted text -


    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.
    raj, Oct 16, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lucky
    Replies:
    11
    Views:
    2,552
    Lucky
    Nov 11, 2005
  2. Salman
    Replies:
    1
    Views:
    411
    Jonathan Bromley
    Apr 5, 2005
  3. Laszlo

    replacing

    Laszlo, Aug 26, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,086
    Peter Jones
    Aug 26, 2003
  4. Ben Holness
    Replies:
    0
    Views:
    5,421
    Ben Holness
    Jan 6, 2006
  5. Rob Meade

    Replacing - and not Replacing...

    Rob Meade, Apr 5, 2005, in forum: ASP General
    Replies:
    5
    Views:
    259
    Chris Hohmann
    Apr 11, 2005
Loading...

Share This Page