12 Replies Latest reply: Dec 15, 2009 2:34 PM by 843789 RSS

    String.replaceAll backslash issue

    843789
      Greetings

      Im trying to replace all the characters ? with the secuence \'ab
      I did tried:
      "a?wd".replaceAll("?","\\'ab");
      and many many others ending with the same result: 'ab

      Thanks in advance for any help you can give me
      Jean Carlo
        • 1. Re: String.replaceAll backslash issue
          843789
          You need to escape the backslashes. Try
          "a?wd".replaceAll("?","\\\\'ab");
          • 2. Re: String.replaceAll backslash issue
            JosAH
            The backslash character isn't just special to the regular expression compiler, it is also special to javac the java compiler in String literals. You have to escape the backslash twice, once for javac and once for the regular expression compiler, so you have to give a replacement String like this: "\\\\'ab"

            kind regards,

            Jos

            Edited: I'm a slow old sod ...
            • 3. Re: String.replaceAll backslash issue
              843789
              Thanks a lot both of you. both of you have correct answers.
              • 4. Re: String.replaceAll backslash issue
                843789
                You're welcome. For my money, Jos's answer is "more" correct than mine, since it carries a more thorough explanation than mine.
                • 5. Re: String.replaceAll backslash issue
                  JosAH
                  Yeah, and I replied first virtually but reality thought otherwise.

                  kind regards,

                  Jos ;-)
                  • 6. Re: String.replaceAll backslash issue
                    843789
                    JosAH wrote:
                    Yeah, and I replied first virtually but reality thought otherwise.

                    kind regards,

                    Jos ;-)
                    The real question is - what prompted Jos's 'road to Damascus' conversion? In just a few short days he has gone from persecution of the believers of the religion of Regex to officiating at the Regex altar. Maybe the 'Pimping Lemon' has just been disproved.
                    • 7. Re: String.replaceAll backslash issue
                      JosAH
                      sabre150 wrote:
                      The real question is - what prompted Jos's 'road to Damascus' conversion? In just a few short days he has gone from persecution of the believers of the religion of Regex to officiating at the Regex altar. Maybe the 'Pimping Lemon' has just been disproved.
                      Has not! There's even a little baby puming lemma for those silly regular languages! The real pumping lemma applies to those grown, strong man context free languages of course! So there.

                      kindest regards,

                      Jos (one can't even match nested matching parentheses with regular expressions, duh ;-)
                      • 8. Re: String.replaceAll backslash issue
                        843789
                        Hi Jos.,

                        You back to 100% health yet?

                        Sabre
                        • 9. Re: String.replaceAll backslash issue
                          JosAH
                          sabre150 wrote:
                          Hi Jos.,

                          You back to 100% health yet?
                          Hi Sabre, I feel fine but I still can hardly walk (no sense of balance) and there is almost no improvement; I guess I have to live with it. Mentally I'm as nutty as I ever was ;-)

                          kind regards,

                          Jos (<--- walks like 'House' with a stick, only worse)
                          • 10. Re: String.replaceAll backslash issue
                            843789
                            JosAH wrote:

                            Hi Sabre, I feel fine but I still can hardly walk (no sense of balance)
                            I'm having trouble walking at the moment - stupidly I fell down the last two stair steps yesterday right onto my face. I have done something to my right leg and since my left leg was already troubling me I now feel as well as look like an old crock. I may have to borrow your stick.
                            and there is almost no improvement; I guess I have to live with it.
                            My brother-in-law had two stokes in the last two years. He is now able to walk with a stick and the 'rest' has allowed him to finish his novel which is to be published early next year.
                            Mentally I'm as nutty as I ever was ;-)
                            :-) I had noticed.

                            >
                            kind regards,

                            Jos (<--- walks like 'House' with a stick, only worse)
                            Sounds just like my brother-in-law. I have heard that that the best medicine for a stroke is 10 bottles of Grolsch a day. You should try it.

                            Regards
                            Sabre
                            • 11. Re: String.replaceAll backslash issue
                              JosAH
                              sabre150 wrote:
                              JosAH wrote:
                              Jos (<--- walks like 'House' with a stick, only worse)
                              Sounds just like my brother-in-law. I have heard that that the best medicine for a stroke is 10 bottles of Grolsch a day. You should try it.
                              You remind me of my quack: he also wanted me to reduce my drinking habits ;-) A curious question: did your brother in law feel the stroke coming? I didn't feel it coming, I didn't notice it happening but all of a sudden I couldn't walk anymore. Those brain strokes are strange things ...

                              kind regards,

                              Jos
                              • 12. Re: String.replaceAll backslash issue
                                843789
                                JosAH wrote:
                                sabre150 wrote:
                                JosAH wrote:
                                Jos (<--- walks like 'House' with a stick, only worse)
                                Sounds just like my brother-in-law. I have heard that that the best medicine for a stroke is 10 bottles of Grolsch a day. You should try it.
                                You remind me of my quack: he also wanted me to reduce my drinking habits ;-)
                                :-)
                                A curious question: did your brother in law feel the stroke coming? I didn't feel it coming, I didn't notice it happening but all of a sudden I couldn't walk anymore. Those brain strokes are strange things ...
                                Like me, Barry just fell down a few stairs (rumour has it that he had had a few Grolsch or the equivalent) but unlike me when he tried to get up he couldn't . In Barry's case they are not sure whether the stroke caused the fall or the other way round. Other than his general way of life, Barry certainly had no warning symptoms.