9 Replies Latest reply: Mar 15, 2008 4:05 PM by 3004 RSS

    White space in regular expressions (Pattern class)

    807601
      Hello,

      I have to check if a String contains ONLY the following characters: a-z, A-Z, ' and ( ) .

      The regular expression that I wanted to use with the Pattern class was [[a-zA-Z][\u0027][\u0028][\u0029][\s]] .

      Now, my compiler (Eclipse) doesnt recognize \s as an expression. Is this the proper expression for a white space?
      Also, in this notation, will the Pattern check for the order of the characters, cause the order isn't supposed to matter.

      I would appreciate any help you could give me on this subject.

      With Best Regards, Roderick
        • 1. Re: White space in regular expressions (Pattern class)
          807601
          http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html
          • 2. Re: White space in regular expressions (Pattern class)
            807601
            Well I just found out that the regular expression for a space is just a space (" ").
            But I still havent found it wether or not the order in the regular expression I provided, now updated to:String regularExprWithSpace = "[[a-zA-Z][\u0027][\u0028][\u0029][ ]]" , is relevant

            I want to know if he will consider Strings like "Jack's linnen is cleaned" correct, eventhough in the regular expression string it is written in the order of a-Z followed by ' ( ) and space.

            Thanks for the API link, but I had already found that.
            • 3. Re: White space in regular expressions (Pattern class)
              DarrylBurke
              I'm not a regex expert, but I don't see any of the regex gurus online, and this I can tell you.
              my compiler (Eclipse) doesnt recognize \s as an expression
              You need to escape the backspace in a String literal. Use
              "\\s"
              in this notation, will the Pattern check for the order of the characters
              For that I think (note:think, not know) you need to put the entire set of characters to be matched in one character class. Could you try this and post back whether it works for your requirement?
              "[^a-zA-Z'()\\s]"
              Note the negation operator at the start of the characer class, which will match positive for any character not in the character class.

              db

              edit You can test your regex here:
              {color:0000ff}http://www.dotnetcoders.com/web/Learning/Regex/RegexTester.aspx{color}
              but remember to double the backslashes whe you include it in your java code as a String literal.

              Edited by: Darryl.Burke
              • 4. Re: White space in regular expressions (Pattern class)
                807601
                Darryl.Burke wrote:
                in this notation, will the Pattern check for the order of the characters
                For that I think (note:think, not know) you need to put the entire set of characters to be matched in one character class.
                im fairly sure [abc] == [[a] [ b ] [c] ]
                [a-d[m-p]]       a through d, or m through p: [a-dm-p] (union)
                edit You can test your regex here:
                or with this:
                http://forum.java.sun.com/thread.jspa?threadID=771359&messageID=4395122
                • 5. Re: White space in regular expressions (Pattern class)
                  3004
                  Royo wrote:
                  Hello,

                  I have to check if a String contains ONLY the following characters: a-z, A-Z, ' and ( ) .
                  if (str.matches("[a-zA-Z'()]*")) {
                    // str is 0 or more characters, and contains nothing other than those characters
                  }
                  If it has to be at least one character, then change the * to a +.

                  You also use
                  "[\\p{Alpha}'()]*"
                  EDIT: Oh, you need whitespace too? then just add a space or
                  \\s
                  inside the square brackets, depending on whether you want to accept only the space character or all whitespace (as defined by the docs for java.util.regex.Pattern).

                  Edited by: jverd on Mar 15, 2008 12:45 PM

                  Edited by: jverd on Mar 15, 2008 12:46 PM
                  • 6. Re: White space in regular expressions (Pattern class)
                    DarrylBurke
                    The OP states
                    I have to check if a String contains ONLY the following characters
                    Wouldn't that require a negation at the start of the character class and testing for no match? Wouldn't
                    "[\\p{Alpha}'()\\s]*"
                    match even when other characters like digits are present?

                    Or is my understanding more flawed than I;d like it to be ;-)

                    Thanks in advance for the next few responses, Darryl
                    • 7. Re: White space in regular expressions (Pattern class)
                      3004
                      Darryl.Burke wrote:
                      The OP states
                      I have to check if a String contains ONLY the following characters
                      Wouldn't that require a negation at the start of the character class and testing for no match?
                      Um, no.
                      if (str.matches(...)) {
                        // it contains only those characters
                      }
                      else {
                        // it contains other characters
                      }
                      Wouldn't
                      "[\\p{Alpha}'()\\s]*"
                      match even when other characters like digits are present?
                      No. \p{Alpha} is A-Za-z.
                      • 8. Re: White space in regular expressions (Pattern class)
                        DarrylBurke
                        Thanks Jeff.

                        I've learned that Pattern.matches doesn't work the same as the site I linked to earlier. Can't edit that post to remove the url unfortunately.

                        That site reports a match for regex pattern [a-zA-Z'()\s]* and test string abg1df. Pattern.matches returns false.

                        db
                        • 9. Re: White space in regular expressions (Pattern class)
                          3004
                          Darryl.Burke wrote:
                          Thanks Jeff.

                          I've learned that Pattern.matches doesn't work the same as the site I linked to earlier. Can't edit that post to remove the url unfortunately.

                          That site reports a match for regex pattern [a-zA-Z'()\s]* and test string abg1df. Pattern.matches returns false.
                          Right, because the digit 1 is not covered by the regex.