Allowing spaces in regular expression match.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    Allowing spaces in regular expression match.

    I'm asking the question for once!

    Say I have the expression @[^a-zA-Z0-9_-]@ that matches anything other than the characters specified. I also want spaces to be allows, including \n, \t, etc. I've tried adding a simple space into the expression such as @[^a-zA-Z0-9_- ]@, but that doesn't seem to work.

    Anyone know how to do this? I could just match any disallowed characters, but I'd rather go this way.

    Thanks,
    Mark(us).
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    I don't get it.

    If you are specifying the characters that are *not* allowed, the expression should allow all white-spaces.

    If you want them to be excluded, try adding a \s into the expression. That represents all white-space characters.

    Maybe I'm just not getting the problem.
    Haven't had any coffee today :P

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by Atli
      I don't get it.

      If you are specifying the characters that are *not* allowed, the expression should allow all white-spaces.

      No, no. I'm specifying that characters that are allowed.

      Originally posted by Atli
      If you want them to be excluded, try adding a \s into the expression. That represents all white-space characters.
      Ah, there you go! Thanks, Atli.

      Originally posted by Atli
      Maybe I'm just not getting the problem.
      Haven't had any coffee today :P
      You solved the problem, even when you didn't understand!

      :D

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ahh ok. I see now :)

        Although, in your expression [^a-zA-Z0-9_-], I always thought the ^ char inverted the class... meaning that it represented everything but the specified chars.

        That was using the Perl-compatible regex tho. Maybe your using something else?

        Then again. I might still not be understanding.
        (Still haven't had any coffee... trying to quit :P)

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Originally posted by Markus
          I've tried adding a simple space into the expression such as @[^a-zA-Z0-9_- ]@, but that doesn't seem to work.
          The hyphen has special meaning inside of square brackets, which might be what threw it off. Try moving the space before the hyphen, e.g.:

          Code:
          @[^a-zA-Z0-9_ -]@
          Alternatively, you can simply escape the hyphen:
          Code:
          @[^a-zA-Z0-9_\- ]@

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by Atli
            Ahh ok. I see now :)

            Although, in your expression [^a-zA-Z0-9_-], I always thought the ^ char inverted the class... meaning that it represented everything but the specified chars.

            That was using the Perl-compatible regex tho. Maybe your using something else?

            Then again. I might still not be understanding.
            (Still haven't had any coffee... trying to quit :P)
            Well, yeah. The characters that are in the regex are those that are allowed. The ^ character checks for anything other than the specified characters.

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by pbmods
              The hyphen has special meaning inside of square brackets, which might be what threw it off. Try moving the space before the hyphen, e.g.:

              Code:
              @[^a-zA-Z0-9_ -]@
              Alternatively, you can simply escape the hyphen:
              Code:
              @[^a-zA-Z0-9_\- ]@
              I'll give it a look. Thanks, Pb.

              Comment

              Working...