Replace "+" with "\\+"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bsonline
    New Member
    • Oct 2007
    • 21

    Replace "+" with "\\+"

    I want to replace + sign with 2 blackslash and 1 + sign (\\+)...
    when I use the replaceAll()...
    It throws a 500 status error like
    "java.util.rege x.PatternSyntax Exception: Dangling meta character '+' near index 0"

    wht s the solution or how can I replace ????
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by bsonline
    I want to replace + sign with 2 blackslash and 1 + sign (\\+)...
    when I use the replaceAll()...
    It throws a 500 status error like
    "java.util.rege x.PatternSyntax Exception: Dangling meta character '+' near index 0"

    wht s the solution or how can I replace ????
    Bit of a nasty one that

    [CODE=java]replaceAll("\\+ ", "\\\\\\\\+" )[/CODE]

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by r035198x
      Bit of a nasty one that

      [CODE=java]replaceAll("\\+ ", "\\\\\\\\+" )[/CODE]
      How does it work can you explain?

      Debasis Jana

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by dmjpro
        How does it work can you explain?

        Debasis Jana
        Remember that + is a special character and \ is a special-special character.
        To match one \ you need four \

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          I'm not a big fan of regex (They're why I run screaming from PERL, but then isn't that known as a "write once" language because you don't ever want to read your own code?).

          Anyhowdy, if you are just replacing text, there is no need to use String's replaceAll method which drags regex patterns into the fray. Use String's replace method which simply replaces all occurrences of the first string with the second.

          [CODE=Java]result = s.replace("+", "\\\\+");[/CODE]

          (You still need to escape \ as you always do in any string, of course.)

          String.replace API

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by BigDaddyLH
            I'm not a big fan of regex (They're why I run screaming from PERL, but then isn't that known as a "write once" language because you don't ever want to read your own code?).

            Anyhowdy, if you are just replacing text, there is no need to use String's replaceAll method which drags regex patterns into the fray. Use String's replace method which simply replaces all occurrences of the first string with the second.

            [CODE=Java]result = s.replace("+", "\\\\+");[/CODE]

            (You still need to escape \ as you always do in any string, of course.)

            String.replace API
            Why do all mathematicians seem to have some sort of history with Perl ...

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              Originally posted by r035198x
              Why do all mathematicians seem to have some sort of history with Perl ...
              Maybe it's being relegated to linux/UNIX machines where it's more likely you'll bump into it. That's what happened to me.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by BigDaddyLH
                Maybe it's being relegated to linux/UNIX machines where it's more likely you'll bump into it. That's what happened to me.
                Until now I managed to stay as far away as possible from Larry Wall's miscarriage.
                I only trust that Perl as far as i can throw a rock at it.

                kind regards,

                Jos

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by JosAH
                  Until now I managed to stay as far away as possible from Larry Wall's miscarriage.
                  I only trust that Perl as far as i can throw a rock at it.

                  kind regards,

                  Jos
                  I'm scared to look at the Perl forum to see what they're writing about us!

                  Comment

                  Working...