Regular expression question!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pro
    New Member
    • Jun 2007
    • 3

    #1

    Regular expression question!

    Hi everyone,
    I would like to ask something about regular expressions in java.

    I have made a code lika that

    Code:
    cleanString = cleanString.replaceAll(";", " ; ");
    cleanString = cleanString.replaceAll(":", " : ");
    cleanString = cleanString.replaceAll(",", " , ");
    cleanString = cleanString.replaceAll("`", " ` ");
    cleanString = cleanString.replaceAll("!", " ! ");
    Can I do it this with only one regular expression, because it seems very bad code.

    I know that the first parameter could be
    replaceAll("[;:,`!]", ) but the second parameter?



    I would like the same for the expression

    Code:
    cleanString = cleanString.replaceAll("That's", " That is ");
    cleanString = cleanString.replaceAll("that's", " that is ");
    Thanks a lot
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by pro
    Hi everyone,
    I would like to ask something about regular expressions in java.

    I have made a code lika that

    Code:
    cleanString = cleanString.replaceAll(";", " ; ");
    cleanString = cleanString.replaceAll(":", " : ");
    cleanString = cleanString.replaceAll(",", " , ");
    cleanString = cleanString.replaceAll("`", " ` ");
    cleanString = cleanString.replaceAll("!", " ! ");
    Can I do it this with only one regular expression, because it seems very bad code.

    I know that the first parameter could be
    replaceAll("[;:,`!]", ) but the second parameter?



    I would like the same for the expression

    Code:
    cleanString = cleanString.replaceAll("That's", " That is ");
    cleanString = cleanString.replaceAll("that's", " that is ");
    Thanks a lot
    Read all about it in the Matcher class; for your example it would look like this:

    [code=java]
    cleanString= cleanString.rep laceAll("[;:,`!]", " $0 ");
    [/code]
    Note the spaces around the $0. The $ group designator refer to parts of the
    previous regular expression or to the entire thing (denoted by $0).

    kind regards,

    Jos

    Comment

    • pro
      New Member
      • Jun 2007
      • 3

      #3
      Thank you very much!

      I would like also to ask what about brackets

      How can I reaplace with something else . . ?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by pro
        Thank you very much!

        I would like also to ask what about brackets

        How can I reaplace with something else . . ?
        I'm sorry, I don't understand your question; brackets and parentheses can be
        escaped by backslashes but I don't know if that's what you're looking for.

        kind regards,

        Jos

        Comment

        • pro
          New Member
          • Jun 2007
          • 3

          #5
          Originally posted by JosAH
          I'm sorry, I don't understand your question; brackets and parentheses can be
          escaped by backslashes but I don't know if that's what you're looking for.

          kind regards,

          Jos
          I found it, is
          cleanString = cleanString.rep laceAll("[\\)\\(\\[\\]]", " $0 ");

          But I didn't understand how can I replace strings that I don't care if the characters are little or big.
          I mean That's,that's,T hAT's to be all the same.

          replaceAll("... ",)????

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by pro
            I found it, is
            cleanString = cleanString.rep laceAll("[\\)\\(\\[\\]]", " $0 ");

            But I didn't understand how can I replace strings that I don't care if the characters are little or big.
            I mean That's,that's,T hAT's to be all the same.

            replaceAll("... ",)????
            Read about all the posibilities in the Pattern API documentation; maybe a
            simple \w will do for your purpose or a POSIX character class ...

            kind regards,

            Jos
            Last edited by JosAH; Jun 10 '07, 09:27 PM. Reason: I can't type; corrected a typo

            Comment

            • prometheuzz
              Recognized Expert New Member
              • Apr 2007
              • 197

              #7
              Originally posted by pro
              I would like the same for the expression

              Code:
              cleanString = cleanString.replaceAll("That's", " That is ");
              cleanString = cleanString.replaceAll("that's", " that is ");
              Try something like this:
              Code:
              String s1 = "ThAt'S it!";
              String s2 = s1.replaceAll("[T|t][H|h][A|a][T|t]'[S|s]", "That is");
              Details:

              Comment

              Working...