#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2

    Check for only zeros and whitespaces


    Hello,
    I want a pattern which checks if a string contains only zeros and whitespaces. Basically,
    Code:
    00 00 00 - valid string
    0a 00 kc - invalid string
    I know how to check for zeros using this, if($userinput =~ /\A[0]+\z/i), but how do I add whitespaces to this ?
    I tried googling for it and found if($str !~ /\S/), but when I put the 2 together, it obviously doesn't work as they coontradict each other.

    Please advise.
    Thanks.
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,264
    Rep Power
    1810
    \S means non-whitespace, which is why they negated it. You need the lower-case version: \s.

    Also, \Z is probably better for your needs than \z, unless you chomp. Traditional perl regexes use ^ and $ for beginning and end of a string, but \A and \Z are fine.

    The character class represented by square brackets is where you would put individual characters you want to match on. You could use
    Code:
    [0 ]
    which is a zero followed by a space, and achieve what you want, but if you feel there could be other types of whitespace, the \s class works.

    Code:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    while (<DATA>) {
    	if(/\A[0\s]+\Z/) {
    		print "valid\n";
    	} else {
    		print "invalid\n";
    	}
    }
    
    __DATA__
    00 00 00
    0a 00 kc

    Comments on this post

    • Laurent_R agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by keath
    You could use [0 ]

    Code:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    while (<DATA>) {
    	if(/\A[0\s]+\Z/) {
    		print "valid\n";
    	} else {
    		print "invalid\n";
    	}
    }
    
    __DATA__
    00 00 00
    0a 00 kc
    Thanks a lot keath, both the methods worked
    I would like to mark the thread as solved. I'm not able to do it under ThreadTools.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    842
    Rep Power
    496
    Even though the solution given by Keath is perfect, I would say that:

    Perl Code:
    /^[0\s]+$/


    would be a more commonly used pattern for what you want to do.

    Or:

    Perl Code:
    /^[0\s]*$/


    if you also want to match an empty line.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    Also, a very tightly written regex could be:
    Code:
    /^(00 ?)+$/
    Assuming you don't have lines like this: 000000
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    39
    Rep Power
    2
    Originally Posted by Laurent_R
    /^[0\s]+$/
    Originally Posted by noobie1000
    Also, a very tightly written regex could be:
    /^(00 ?)+$/
    Thanks a lot Laurent_R, noobie1000 for your replies. I did try them out and it worked.

IMN logo majestic logo threadwatch logo seochat tools logo